0

我尝试创建一个代码,每次加载组件时增加 2%,但我没有显示该数字增量,而是得到NaN. 这是为什么?

//Define variable
var PercentLoaded = 0;

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

感谢大家的帮助!

4

2 回答 2

4

增加变量时尽量不要重新声明变量:

//Define variable
var PercentLoaded = 0;

//When a component loads
/*var*/ PercentLoaded = PercentLoaded + 2;
于 2012-06-23T15:47:20.217 回答
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
}
于 2012-06-23T15:52:38.240 回答