当我尝试在.each函数之外使用变量时,变量变为Nan [英] Variable becomes Nan when i try to use it outside of the .each function

查看:146
本文介绍了当我尝试在.each函数之外使用变量时,变量变为Nan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm grabbing a JSON obj from an external source. It appears as so:

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

我将其设置为全局var,将其设置在每个函数中,然后尝试在下面的外部对其进行检索.但是我得到了Nan作为价值.该值肯定是在函数中设置的,因此我不完全确定为什么会发生这种情况.

I set it as a global var, set it in the each function and then try to retrieve it outside of it, below. But i get a Nan as the value. The value is deifinitely being set in the function so i am not entirely sure why this is happening.

感谢您的帮助!

$( 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();
    });

推荐答案

在if语句var todaystart;

 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是异步函数

NOTE: Move the calculation code inside getJSON method bcoz getJSON is Asynchronous function

这篇关于当我尝试在.each函数之外使用变量时,变量变为Nan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆