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
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() { ... })).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.
The timeout ID is by design completely opaque. The only defined interface that can do anything with it is the
clearTimeout/clearIntervalcall. There is no interface provided to get the function back from a timeout ID.