出于某种原因,我将js代码包装在一个立即调用的函数表达式中,我得到了progressBar not defined
.想知道为什么会这样吗?
(function(){ "use strict" var progressBar = function(){ var bar = document.getElementById('pbar'), status = document.getElementById('status'), barValue = bar.value; status.innerHTML = barValue + "%"; bar.value++; var increment = setTimeout("progressBar()", 50); if(bar.value == 100){ status.innerHTML = '100% - Straight Besting'; bar.value = '100'; clearTimeout(increment); } } progressBar(); })()
noahnu.. 5
将字符串传递给setTimeout
它时,将在全局window
对象的上下文中调用它.
将setTimeout
行更改为:
var increment = setTimeout(progressBar, 50);
例如
(function() {
let i = 0;
let myfunction = function() {
console.log(i++);
if (i < 10) setTimeout(myfunction, 100);
};
myfunction();
})()
将字符串传递给setTimeout
它时,将在全局window
对象的上下文中调用它.
将setTimeout
行更改为:
var increment = setTimeout(progressBar, 50);
例如
(function() {
let i = 0;
let myfunction = function() {
console.log(i++);
if (i < 10) setTimeout(myfunction, 100);
};
myfunction();
})()