简体   繁体   English

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

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

Example: 例:

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

Is there any way I can call the same function again after that timeout. 有什么办法可以在超时后再次调用相同的函数。 The problem I'm having is that the arguments are not strings, but nodes. 我遇到的问题是参数不是字符串,而是节点。 Eg: 例如:

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

Your question leads me to think that you are trying to do this 你的问题让我觉得你正在努力做到这一点

HTML HTML

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

Javascript 使用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'));

On jsFiddle jsFiddle上

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

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