Let's assume that I have the timeout ID returned from setTimeout
or setInterval
.
Can I get, in some way, the original function or code, associated with it?
Something like this:
var timer_id = setTimeout(function() {
console.log('Hello Stackoverflowers!');
}, 100000);
var fn = timer_id.get_function(); // desired method
fn(); // output: 'Hello Stackoverflowers!'
You can store each timeout function in an object so that you can retrieve it later.
the IDs returned from
setTimeout
/setInterval
are just numbers, they have no properties or methods other than those that every other number would have. If you want to get that function, you can declare it first instead of using an anonymous:You can put a wrapper around
setTimeout
- I just threw this one together (after a few iterations of testing...)NB: this won't work if you use a string for the callback. But no one does that, do they..?
Nor does it support passing the ES5 additional parameters to the callback function, although this would be easy to support.