Javascript 如何在jquery中组合两个函数?

Javascript 如何在jquery中组合两个函数?,javascript,jquery,json,Javascript,Jquery,Json,如何组合这两个函数并在updatePrice函数中发出警报var price?目前,我只能在第一个函数中发出警报price,但我想调用alert(price)insideupdatePrice函数 $(document).ready(function() { var json = (function () { var json = null; $.ajax({ 'async': false, 'global': false, 'url': 'https://api.co

如何组合这两个函数并在
updatePrice
函数中发出警报
var price
?目前,我只能在第一个函数中发出警报
price
,但我想调用
alert(price)
inside
updatePrice
函数

$(document).ready(function()
{

var json = (function () {
var json = null;
$.ajax({
    'async': false,
    'global': false,
    'url': 'https://api.coinmarketcap.com/v1/ticker/ethereum/?convert=USD',
    'dataType': "json",
    'success': function (data) {
        json = data[0].price_usd;
        var price = JSON.stringify(json);
        var price = JSON.parse(price);
        alert(price);
    }
});
})(); 

function updatePrice ()
{
    var ethprice = parseFloat($(".usd-input").val());
    var ethtotal = (ethprice) / 298;
    var ethtotal = ethtotal.toFixed(3);
    if(isNaN(ethtotal)) {
    var ethtotal = "";
    }
    $(".eth-input").val(ethtotal);

    var tvcprice = parseFloat($(".usd-input").val());
    var tvctotal = (tvcprice) / 1;
    var tvctotal = tvctotal.toFixed(3);
    if(isNaN(tvctotal)) {
    var tvctotal = "";
    }
    $(".tvc-input").val(tvctotal);
}
$(document).on("change, keyup", ".usd-input", updatePrice);  

})

将第一个函数(例如,
showart()
)声明为全局函数,然后从updatePrice()函数调用它

var price;     // declare in global scope, so can be accessable from anywhere.

var showAlert = function () {
  var json = null;
  $.ajax({
    'async': false,
    'global': false,
    'url': 'https://api.coinmarketcap.com/v1/ticker/ethereum/?
     convert=USD',
     'dataType': "json",
     'success': function (data) {
      json = data[0].price_usd;
      price = JSON.stringify(json);
      price = JSON.parse(price);
      alert(price);
    }
  });
}

showAlert();
还可以在全局范围内声明
price
变量,因为您希望从
updateFunction()
函数访问它

var price;     // declare in global scope, so can be accessable from anywhere.

var showAlert = function () {
  var json = null;
  $.ajax({
    'async': false,
    'global': false,
    'url': 'https://api.coinmarketcap.com/v1/ticker/ethereum/?
     convert=USD',
     'dataType': "json",
     'success': function (data) {
      json = data[0].price_usd;
      price = JSON.stringify(json);
      price = JSON.parse(price);
      alert(price);
    }
  });
}

showAlert();

现在,只需从updatePrice()函数内部调用showAlert()。

将第一个函数(例如,
showAlert()
)声明为全局函数,然后从updatePrice()函数调用它

$(document).ready(function()
{

var json = (function () {
var json = null;
$.ajax({
    'async': false,
    'global': false,
    'url': 'https://api.coinmarketcap.com/v1/ticker/ethereum/?convert=USD',
    'dataType': "json",
    'success': function (data) {
        json = data[0].price_usd;
        var price = JSON.stringify(json);
        var price = JSON.parse(price);
        alert(price);
    }
});
})(); 

function updatePrice ()
{
    var ethprice = parseFloat($(".usd-input").val());
    var ethtotal = (ethprice) / 298;
    var ethtotal = ethtotal.toFixed(3);
    if(isNaN(ethtotal)) {
    var ethtotal = "";
    }
    $(".eth-input").val(ethtotal);

    var tvcprice = parseFloat($(".usd-input").val());
    var tvctotal = (tvcprice) / 1;
    var tvctotal = tvctotal.toFixed(3);
    if(isNaN(tvctotal)) {
    var tvctotal = "";
    }
    $(".tvc-input").val(tvctotal);
}
$(document).on("change, keyup", ".usd-input", updatePrice);  

})
var price;     // declare in global scope, so can be accessable from anywhere.

var showAlert = function () {
  var json = null;
  $.ajax({
    'async': false,
    'global': false,
    'url': 'https://api.coinmarketcap.com/v1/ticker/ethereum/?
     convert=USD',
     'dataType': "json",
     'success': function (data) {
      json = data[0].price_usd;
      price = JSON.stringify(json);
      price = JSON.parse(price);
      alert(price);
    }
  });
}

showAlert();
还可以在全局范围内声明
price
变量,因为您希望从
updateFunction()
函数访问它

var price;     // declare in global scope, so can be accessable from anywhere.

var showAlert = function () {
  var json = null;
  $.ajax({
    'async': false,
    'global': false,
    'url': 'https://api.coinmarketcap.com/v1/ticker/ethereum/?
     convert=USD',
     'dataType': "json",
     'success': function (data) {
      json = data[0].price_usd;
      price = JSON.stringify(json);
      price = JSON.parse(price);
      alert(price);
    }
  });
}

showAlert();

现在,只需从updatePrice()函数内部调用showAlert()。

如何比较var price与var ethtotal=(ethprice)/price;而不是var ethtotal=(ethprice)/298;?在全局范围内声明
price
变量。更新答案。如何将var价格与var ethtotal=(ethprice)/价格进行比较;而不是var ethtotal=(ethprice)/298;?在全局范围内声明
price
变量。更新答案。
$(document).ready(function()
{

var json = (function () {
var json = null;
$.ajax({
    'async': false,
    'global': false,
    'url': 'https://api.coinmarketcap.com/v1/ticker/ethereum/?convert=USD',
    'dataType': "json",
    'success': function (data) {
        json = data[0].price_usd;
        var price = JSON.stringify(json);
        var price = JSON.parse(price);
        alert(price);
    }
});
})(); 

function updatePrice ()
{
    var ethprice = parseFloat($(".usd-input").val());
    var ethtotal = (ethprice) / 298;
    var ethtotal = ethtotal.toFixed(3);
    if(isNaN(ethtotal)) {
    var ethtotal = "";
    }
    $(".eth-input").val(ethtotal);

    var tvcprice = parseFloat($(".usd-input").val());
    var tvctotal = (tvcprice) / 1;
    var tvctotal = tvctotal.toFixed(3);
    if(isNaN(tvctotal)) {
    var tvctotal = "";
    }
    $(".tvc-input").val(tvctotal);
}
$(document).on("change, keyup", ".usd-input", updatePrice);  

})