本文介绍了setTimeout没有延迟和立即执行函数一样吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在查看Web应用程序中的一些现有代码。我看到了这个:
I am looking at some existing code in a web application. I saw this:
window.setTimeout(function(){...})
这是否只是立即执行函数内容?
Is this the same as just executing the function content right away?
推荐答案
赢了必须立即运行,也不会将延迟显式设置为0.原因是setTimeout从执行队列中删除了该函数,并且只有在JavaScript完成当前执行队列后才会调用它。
It won't necessarily run right away, neither will explicitly setting the delay to 0. The reason is that setTimeout removes the function from the execution queue and it will only be invoked after JavaScript has finished with the current execution queue.
console.log(1);
setTimeout(function() {console.log(2)});
console.log(3);
console.log(4);
console.log(5);
//console logs 1,3,4,5,2
了解更多详情,请参阅
for more details see http://javascriptweblog.wordpress.com/2010/06/28/understanding-javascript-timers/
这篇关于setTimeout没有延迟和立即执行函数一样吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!