JavaScript setTimeout()不会等待执行? [英] JavaScript setTimeout() won't wait to Execute?

查看:145
本文介绍了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>

是什么导致它执行 IMMEDIATELY ,而不是等待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!

Mason

推荐答案

alertBox()

Doesn'这看起来像一个立即函数调用?

Doesn't this look like an immediate function call?

尝试传递函数(不执行它):

Try passing the function (without executing it) instead:

setInterval(alertBox, 5000);

这篇关于JavaScript setTimeout()不会等待执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆