【问题标题】:Variable becomes Nan when i try to use it outside of the .each function当我尝试在 .each 函数之外使用变量时,变量变为 Nan
【发布时间】:2014-09-12 19:43:41
【问题描述】:

我正在从外部来源获取 JSON obj。看起来是这样的:

{"total":16231642,"totalamount":437442282.55}

我将它设置为全局变量,在每个函数中设置它,然后尝试在下面检索它。但我得到一个楠作为价值。该值肯定是在函数中设置的,所以我不完全确定为什么会发生这种情况。

感谢任何帮助!

$( document ).ready(function() {
    var todaystart;
    //Get vals from JSON txt
        $.getJSON( "proxy.php", function( data ) {
        $.each(data, function (key, val) {
            if (key == 'totalamount')
            {
                var todaystart = val; //was using parseFloat before to ensure that the datatype was correct, in case anybody answers that.
                //alert(todaystart);                
            }
        });


        });


            //Total Earned
            var avgvol = 18556;
            var price = 26.95;
            var avg = avgvol * price;
            alert(todaystart);
            var avgpls = todaystart + avg;

            var numAnim = new countUp("totalmon", todaystart, avgpls, 0, 86400);
                numAnim.start();

        //Sess Earned       
            remavgpls = avgpls - todaystart;    
            var nu2Anim = new countUp("sessmon", 0, remavgpls, 0, 86400);
                nu2Anim.start();
        //Sess Time     
            var nu3Anim = new countUp("minmon", 0, 86400, 0, 864000);
                nu3Anim.start();
    });

【问题讨论】:

  • 它只是一个 var 重新声明检查这个fiddle

标签: javascript jquery json nan


【解决方案1】:

去掉if语句var todaystart;中的var关键字

 if (key == 'totalamount')
            {
                todaystart = val; //was using parseFloat before to ensure that the datatype was correct, in case anybody answers that.
                //alert(todaystart);                
            }

您的完整代码将是

$(document).ready(function () {
    var todaystart;
    //Get vals from JSON txt
    $.getJSON("proxy.php", function (data) {
        $.each(data, function (key, val) {
            if (key == 'totalamount') {
                todaystart = val; //was using parseFloat before to ensure that the datatype was correct, in case anybody answers that.
                //alert(todaystart);                
            }
        });

        calcualtion();


    });

});

function calcualtion() {

    var avgvol = 18556;
    var price = 26.95;
    var avg = avgvol * price;
    alert(todaystart);
    var avgpls = todaystart + avg;

    var numAnim = new countUp("totalmon", todaystart, avgpls, 0, 86400);
    numAnim.start();

    //Sess Earned       
    remavgpls = avgpls - todaystart;
    var nu2Anim = new countUp("sessmon", 0, remavgpls, 0, 86400);
    nu2Anim.start();
    //Sess Time     
    var nu3Anim = new countUp("minmon", 0, 86400, 0, 864000);
    nu3Anim.start();
}

注意:将计算代码移到getJSON方法bcoz getJSON是异步函数

【讨论】:

  • 干杯伙伴。很好的答案。
  • @Tapha 很高兴为您提供帮助 :)
【解决方案2】:

每次循环打开时,您都在重新声明变量“todaystart”,应该避免这种情况。永远不要在循环内创建变量,而是将其作为全局变量,以提高客户端性能。

【讨论】:

    猜你喜欢
    • 2022-06-14
    • 2016-04-21
    • 2019-02-05
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    相关资源
    最近更新 更多