admin管理员组文章数量:1202351
I'm trying to get an event to fire after five minutes. I'm using the following code:
setTimeout(tweet(name, type), 5 * 60 * 1000);
It is firing after a while, but not nearly five minutes (Usually two minutes or so, but sometimes it's instant.). What am I doing wrong? (I've also tried setting the time to 300000
instead, same problem.
I'm trying to get an event to fire after five minutes. I'm using the following code:
setTimeout(tweet(name, type), 5 * 60 * 1000);
It is firing after a while, but not nearly five minutes (Usually two minutes or so, but sometimes it's instant.). What am I doing wrong? (I've also tried setting the time to 300000
instead, same problem.
2 Answers
Reset to default 20You are calling tweet
immediately and passing its return value to setTimeout
.
You need to pass a function to setTimeout
. You haven't included the code for tweet
, but I'm going to assume that it doesn't return
a function.
setTimeout(function () { tweet(name, type); }, 5 * 60 * 1000);
Quentin's solution works, but you can also use this form:
setTimeout(tweet(name, type), 5 * 60 * 1000);
function tweet(name, type) {
return function(name, type) {
};
}
It has some use cases when you want to keep some values in a closure.
本文标签: javascriptUsing setTimeout() for large valuesStack Overflow
版权声明:本文标题:javascript - Using setTimeout() for large values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738637709a2104134.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论