javascript - Get function associated with setTimeout or setInterval - Stack Overflow

Let's assume that I have the timeout ID returned from setTimeout or setInterval.Can I get, in some

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!'

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!'
Share Improve this question asked May 2, 2013 at 13:20 franzlorenzonfranzlorenzon 5,9436 gold badges37 silver badges58 bronze badges 1
  • 3 Afaik you'd have to write your own wrapper around setTimeout. – Felix Kling Commented May 2, 2013 at 13:23
Add a ment  | 

4 Answers 4

Reset to default 4

You can put a wrapper around setTimeout - I just threw this one together (after a 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 the map is cleared up on pletion
             fn();
         }, delay);
         cache[id] = fn;
         return id;
     }

     window.clearTimeout = function(id) {
         delete cache[id];
         _clearTimeout(id);
     }

     window.getTimeout = function(id) {
         return cache[id];
     }
})();

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.

var timeouts = {};  // hold the data
function makeTimeout (func, interval) {

    var run = function(){
        timeouts[id] = undefined;
        func();
    }

    var id = window.setTimeout(run, interval);
    timeouts[id] = func;

    return id;
}
function removeTimeout (id) {
    window.clearTimeout(id);
    timeouts[id]=undefined;
}
function doTimeoutEarly (id) {
  func = timeouts[id];
  removeTimeout(id);
  func();
}

var theId = makeTimeout( function(){ alert("here"); }, 10000);
console.log((timeouts[theId] || "").toString());
timeouts[theId](); // run function immediately, will still run with timer

You can store each timeout function in an object so that you can retrieve it later.

var timeout_funcs = {};

function addTimeout(func,time) {
    var id = window.setTimeout(func,time);
    timeout_funcs[id] = func;
    return id;
}

function getTimeout(id) {
    if(timeout_funcs[id])
        return timeout_funcs[id];
    else
        return null;
}

function delTimeout(id) {
    if(timeout_funcs[id]) {
        window.clearTimeout(timeout_funcs[id]);
        delete timeout_funcs[id];
    }
}

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:

var myFunc = function() {
    console.log('Hello Stackoverflowers!');
};

var timer_id = setTimeout(myFunc, 100000);

myFunc(); // output: 'Hello Stackoverflowers!'

clearTimeout(timer_id); // unless you want it to fire twice

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744084794a4555968.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信