[英]Not sure why this isn't working - Javascript basic increment function
I am writing code to increment the most basic progress bar ever...... it just isn't working. 我正在编写代码以增加有史以来最基本的进度条……这是行不通的。 These are the variable used:
这些是使用的变量:
map.progress_bar = // the progress div that grows inside a container div that is the width of the screen (100%);
progress = 0;
state.window_width = // the width of the window or otherwise $(window).width();
setTimeout(function incProgress(){
if ( map.progress_bar.width() < state.window_width ) {
progress += 10;
map.progress_bar.css({
width: String(progress + '%')
});
console.log('progress: ', map.progress_bar.width());
console.log('window: ', state.window_width);
setTimeout(incProgress(), 300);
}
}, 300);
Please do not ask me to do setInterval. 请不要让我做setInterval。 Please explain to me why on earth this does not work, I feel very unhappy.
请向我解释为什么这在地球上不起作用,我感到非常不高兴。
setTimeout(incProgress(), 300);
You are calling the function and passing its return value ( undefined
) to setTimeout
. 您正在调用该函数,并将其返回值 (
undefined
)传递给setTimeout
。
You need to pass a function to setTimeout
. 您需要将一个函数传递给
setTimeout
。
Remove the ()
. 删除
()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.