JavaScript setTimeout() 不会等待执行? [英] JavaScript setTimeout() won't wait to Execute?
本文介绍了JavaScript setTimeout() 不会等待执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下示例:
<script type="text/javascript">
function alertBox(){
alert('Hello World!');
}
function doSomething(){
setInterval(alertBox(), 5000); //This is for generic purposes only
};
function myFunction(){
setTimeout(doSomething(),3000);
};
myFunction();
</script>
是什么导致它立即执行,而不是等待设定的 3 秒,以及只执行警报ONCE,而不是预定的 5 秒间隔?
What is it that causes this to execute IMMEDIATELY, rather than waiting the 3 seconds set, as well as only executing the alert ONCE, rather than at the scheduled 5 second intervals?
感谢您提供的任何帮助!
Thanks for any help you can provide!
梅森
推荐答案
alertBox()
这看起来不像是一个直接的函数调用吗?
Doesn't this look like an immediate function call?
尝试传递函数(不执行):
Try passing the function (without executing it) instead:
setInterval(alertBox, 5000);
这篇关于JavaScript setTimeout() 不会等待执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文