tags:

views:

104

answers:

2

i am wondering if i can do some cleanup routines that will auto grab timeouts / intervals. consider this:

var timeout = setInterval(function dimitar() {
    console.log("hi!");
}, 1000);

console.log(window);

i had a look through window and can't find any reference to the function that's been passed on. the reference to timeout is there, sure enough. so where does the function 'live' here? is it launching a new instance of the js interpreter to eval/run/keep the code? how can you access it in relation to the timeout uid?

i know i can curry the setInterval function and get it to always store the reference into an array which i can then loop through and clear, but am curious if there's a natural way of doing this

+2  A: 

The function you create in your example is using a named function expression. The name is only available within the function. Otherwise, it behaves the same as an anonymous function: you haven't assigned it to a variable and since it's not a function declaration, it doesn't create a dimitar variable in the enclosing scope. The following article may be useful: http://yura.thinkweb2.com/named-function-expressions/

There's no eval-type thing going on: you've just passed in a reference to a function into window.setInterval. This function cannot be retrieved afterwards unless you've assigned it to a variable previously, or it was a reference to a function defined by a function declaration.

If you want to keep a reference to the function around, it's simply a matter of storing it in a variable first:

var dimitar = function() {
    console.log("hi!");
};

window.setInterval(dimitar, 1000);
Tim Down
thanks for the comments - as i said - i only put dimitar there so i can search for it within firebug. the reference to `window.dimitar` is not the one that the setInterval instance is running. if you do `window.dimitar = null;` the console will continue running as the function has been passed already.
Dimitar Christoff
+1  A: 

so where does the function 'live' here?

The timeout/interval queue is an internal implementation detail that's not accessible to content JavaScript. It retains a reference to the function passed in to setInterval, but it's not a reference that's visible to you.

Incidentally you should generally avoid using named inline function expressions. Although it's probably OK in this example code, IE's JScript has some serious basic bugs with them that can trip you up if you're not careful. Stick to named function statements (function dimitar() { ... } ... setInterval(dimitar, 1000)) or anonymous inline function expressions (setInterval(function() { ... })).

is it launching a new instance of the js interpreter to eval/run/keep the code?

No, it's the same interpreter and the queue could even be implemented in JavaScript. But the variables behind it are hidden away from the caller.

how can you access it in relation to the timeout uid?

The timeout ID is by design completely opaque. The only defined interface that can do anything with it is the clearTimeout/clearInterval call. There is no interface provided to get the function back from a timeout ID.

bobince
thanks for the explanation, it's much more clear now - in other words, i need to keep and clean the references instead. i only put the named function dimitar there so i can search for it in firebug.
Dimitar Christoff
‘clean’ as in remove the timeouts `onunload` to try to avoid memory leaks? This should only be necessary for IE6. (Even then, I'm not wholly sure that timeouts participate in IE's infamous native/host-object reference loops, haven't tested it.)
bobince
i just built this in mootools - http://mootools.net/shell/judGJ/ and yes, avoiding memory leaks due to code that may have been left running is not a bad idea, imo - particularly so for IE
Dimitar Christoff