Variable becomes Nan when i try to use it outside of the .each function

偶尔善良 提交于 2019-12-12 04:46:56

问题


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

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

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.

Any help is appreciated!

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

回答1:


Remove var key word inside if statement 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);                
            }

Your full code will be

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

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




回答2:


You are re declaring the variable 'todaystart' each time the loop opens, which should be avoided. Never ever create a variable inside loops, instead make it as a global variable, to increase the client-side performance.



来源:https://stackoverflow.com/questions/24879728/variable-becomes-nan-when-i-try-to-use-it-outside-of-the-each-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!