Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call an anonymous function defined in a setInterval

Tags:

javascript

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.

like image 838
Tominator Avatar asked Jun 03 '10 08:06

Tominator


People also ask

How do you call a setInterval function?

The setInterval() method calls a function at specified intervals (in milliseconds). The setInterval() method continues calling the function until clearInterval() is called, or the window is closed. 1 second = 1000 milliseconds.

How are anonymous functions called?

In Python, an anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.

How do you pass parameters to setInterval?

The setInterval() method can pass additional parameters to the function, as shown in the example below. setInterval(function, milliseconds, parameter1, parameter2, parameter3); The ID value returned by setInterval() is used as the parameter for the clearInterval() method.

Why you should not use setInterval?

In case of time intensive synchronous operations, setTimeInterval may break the rhythm. Also, if any error occurs in setInterval code block, it will not stop execution but keeps on running faulty code. Not to mention they need a clearInterval function to stop it.


3 Answers

You 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.

like image 175
Daniel Vassallo Avatar answered Sep 28 '22 08:09

Daniel Vassallo


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.

like image 39
Mathias Bynens Avatar answered Sep 28 '22 08:09

Mathias Bynens


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.

like image 39
DiverseAndRemote.com Avatar answered Sep 28 '22 07:09

DiverseAndRemote.com