繁体   English   中英

有没有办法在JavaScript中超时之后再次调用当前函数(及其参数)?

[英]Is there a way call the current function (and it's arguments) again after a Timeout in JavaScript?

例:

function foo(){
setTimeout(/*function(){foo(and all arguments);}*/, 2000);
}

有什么办法可以在超时后再次调用相同的函数。 我遇到的问题是参数不是字符串,而是节点。 例如:

foo(document.body, document.getElementById('header'));

你的问题让我觉得你正在努力做到这一点

HTML

<div id="header"></div>

使用Javascript

function foo() {
    setTimeout((function (that, args) {
        return function () {
            foo.apply(that, args);
        };
    }(this, arguments)), 2000);

    console.log(arguments);
}

foo(document.body, document.getElementById('header'));

jsFiddle上

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM