Oct 16, 2009

later method

Object.prototype.later = function (msec, method) {
    var context = this,
    args = Array.prototype.slice.apply(arguments, [2]); 
    if (typeof method === 'string') { 
        method = context[method]; 
    } 
    setTimeout(function () { 
        method.apply(context, args); 
    }, msec); 
    return context; 
}; 

var o = {
  name: "fred",
  greet: function (msg) {
          alert("my name is " + this.name + "," + msg);
  }
}
    
o.later(1000, function () { alert("hello");});
o.later(1000, "greet", "how are you?");
​