admin管理员组文章数量:1195608
I've made this code:
window.setInterval(function(){ var a = doStuff(); var b = a + 5; }, 60000)
The actual contents of the anonymous function is of course just for this small example as it doesn't matter. What really happens is a bunch of variables get created in the scope of the function itself, because I don't need/want to pollute the global space.
But as you all know, the doStuff() function won't be called until 60 seconds in the page. I would also like to call the function right now, as soon as the page is loaded, and from then on every 60 seconds too.
Is it somehow possible to call the function without copy/pasting the inside code to right after the setInterval() line? As I said, I don't want to pollute the global space with useless variables that aren't needed outside the function.
I've made this code:
window.setInterval(function(){ var a = doStuff(); var b = a + 5; }, 60000)
The actual contents of the anonymous function is of course just for this small example as it doesn't matter. What really happens is a bunch of variables get created in the scope of the function itself, because I don't need/want to pollute the global space.
But as you all know, the doStuff() function won't be called until 60 seconds in the page. I would also like to call the function right now, as soon as the page is loaded, and from then on every 60 seconds too.
Is it somehow possible to call the function without copy/pasting the inside code to right after the setInterval() line? As I said, I don't want to pollute the global space with useless variables that aren't needed outside the function.
Share Improve this question asked Jun 3, 2010 at 8:02 TominatorTominator 1,2243 gold badges20 silver badges37 bronze badges 2- Create the function outside the interval and put it into a variable? – Pekka Commented Jun 3, 2010 at 8:04
- 2 I know, but that's what I'm trying to avoid, if possible. – Tominator Commented Jun 3, 2010 at 8:08
3 Answers
Reset to default 18You can put your callback function in a variable, and wrap up everything in a self-invoking anonymous function:
(function () {
var callback = function() {
var a = doStuff();
var b = a + 5;
};
callback();
window.setInterval(callback, 60000);
})();
No pollution.
This is possible without creating global variables as well:
setInterval((function fn() {
console.log('foo'); // Your code goes here
return fn;
})(), 5000);
Actually, this way, you don’t create any variables at all.
However, in Internet Explorer, the fn
function will become accessible from the surrounding scope (due to a bug). If you don’t want that to happen, simply wrap everything in a self-invoking anonymous function:
(function() {
setInterval((function fn() {
console.log('foo'); // Your code goes here
return fn;
})(), 5000);
})();
Credit to Paul Irish for sharing this trick.
Edit: Answer updated with some more information, thanks to bobince.
yet another solution:
(function() {
var a = doStuff();
var b = a + 5;
window.setTimeout(arguments.callee, 60000);
})();
This uses timeout instead of interval so that it can run the first time and then run it's self again after a timeout.
本文标签: javascriptCall an anonymous function defined in a setIntervalStack Overflow
版权声明:本文标题:javascript - Call an anonymous function defined in a setInterval - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738438073a2086793.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论