javascript - Get function associated with setTimeout or setInterval -
let's assume have timeout id returned settimeout
or setinterval
.
can get, in way, original function or code, associated it?
something 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 put wrapper around settimeout
- threw 1 (after few iterations of testing...)
(function() { var cache = {}; var _settimeout = window.settimeout; var _cleartimeout = window.cleartimeout; window.settimeout = function(fn, delay) { var id = _settimeout(function() { delete cache[id]; // ensure map cleared on completion fn(); }, delay); cache[id] = fn; return id; } window.cleartimeout = function(id) { delete cache[id]; _cleartimeout(id); } window.gettimeout = function(id) { return cache[id]; } })();
nb: won't work if use string callback. no 1 that, they..?
nor support passing es5 additional parameters callback function, although easy support.
Comments
Post a Comment