NaN而不是我的加载问题的数字

时间:2012-06-23 15:45:03

标签: jquery

我尝试创建一个代码,每次加载一个组件时会增加2%,但是不是显示该数字增量,而是NaN。那是为什么?

//Define variable
var PercentLoaded = 0;

//When a component loads
var PercentLoaded = PercentLoaded+2;
//Print value
$('#LoadProgressMessage').html('<h6>'+ PercentLoaded + '% Loaded</h6>');

感谢大家的帮助!

2 个答案:

答案 0 :(得分:4)

尝试在递增变量时不重新声明变量:

//Define variable
var PercentLoaded = 0;

//When a component loads
/*var*/ PercentLoaded = PercentLoaded + 2;

答案 1 :(得分:1)

很可能变量不再在范围内。您提供的代码本身应该可以正常工作,但您可能在实际代码中有类似的东西:

$( function() {
    var PercentLoaded = 0;

    // PercentLoaded is in scope only up to here
});

function whatever() {
    var PercentLoaded = PercentLoaded + 2;  
   // The original PercentLoaded is no longer in scope
}