farzad
farzad

Reputation: 8855

how to write a recursive method in JavaScript using window.setTimeout()?

I'm writing a JavaSCript class that has a method that recursively calls itself.

Scheduler.prototype.updateTimer = function () {
    document.write( this._currentTime );
    this._currentTime -= 1000;
    // recursively calls itself
    this._updateUITimerHandler = window.setTimeout( arguments.callee , 1000 );
}

Property description:

_currentTime: the currentTime of the timer in miliseconds.
_updateUITimerHandler: stores the reference so can be used later with clearTimeout().

my problem is where I'm using recursion with setTimeout(). I know setTimeout() will accept some string to execute, or a reference to a function. since this function is method of an object, I don't know how to call it from outside. so I used the second format of setTimeout() and passed in a reference to the method itself. but it does not work.

Upvotes: 1

Views: 6359

Answers (3)

annakata
annakata

Reputation: 75794

Well the first thing to say is that if you're calling setTimeout but not changing the interval, you should be using setInterval.

edit (update from comment): you can keep a reference from the closure if used as a class and setInterval/clearInterval don't require re-referencing.

edit2: it's been pointed out that you wrote callee which will work quite correctly and 100% unambiguously.

Out of completeness, this works:

function f() 
{
  alert('foo');
  window.setTimeout(arguments.callee,5000);
}

f();

so I tried out document.write instead of alert and that is what appears to be the problem. doc.write is fraught with problems like this because of opening and closing the DOM for writing, so perhaps what you needed is to change the innerHTML of your target rather than doc.write

Upvotes: 1

AnthonyWJones
AnthonyWJones

Reputation: 189457

Try this:-

Scheduler.prototype.startTimer = function() {
  var self = this;
  function updateTimer() {
    this._currentTime -= 1000;
    self.hTimer = window.setTimeout(updateTimer, 1000)
    self.tick()
  }
  this.hTimer = window.setTimeout(updateTimer, 1000)
}
Scheduler.prototype.stopTimer = function() {
    if (this.hTimer != null) window.clearTimeout(this.hTimer)
  this.hTimer = null;
}
Scheduler.prototype.tick = function() {
  //Do stuff on timer update
}

Upvotes: 9

Pablo Cabrera
Pablo Cabrera

Reputation: 5849

You could hold a pointer towards it...

/* ... */
var func = arguments.callee;
this._updateUITimerHandler = window.setTimeout(function() { func(); }, 1000);
/* ... */

Upvotes: 0

Related Questions