Jak wywołać metodę obiektu z użyciem setTimeout

piechnat

Można posłużyć się taką funkcją:

Function.prototype.setApply = function(delay, thisArg, argArray) {
  var thisFunc = this;
  argArray = (argArray instanceof Array) ? argArray : [];
  return setTimeout( function() {
    thisFunc.apply(thisArg, argArray);
  }, delay);
};

Przykład użycia:

function TestClass() {
  this.showMsg = function(str) {
    alert(str);
  }
  this.delayedMsg = function(str) {
    this.showMsg.setApply(1000, this, [str]);
  }
}
var testObj = new TestClass();
testObj.delayedMsg('Test');
FAQ

0 komentarzy