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

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 }.

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 }.

Awsome answer. Thanks. – farzad Dec 10 '08 at 12:22 +1 -- @farzad: I think this deserves your up-vote as well (not just your "accepted" tick).

;-) – Tomalak Dec 10 '08 at 13:13.

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 calle*e* 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.

Thanks for the advise. Using the setInterval() is a much better choice. Although becuase I'm writing this calss as a library, I wouldn't know the name of the Objects beeing instanciated from my class.So I can not call them inside my own class.

– farzad Dec 10 '08 at 11:49 Aren't you confusing callee with caller? – AnthonyWJones Dec 10 '08 at 12:06 ha - quite right I am, calle*e* works absolutely fine, will amend – annakata Dec 10 '08 at 13:04.

You could hold a pointer towards it... /* ... */ var func = arguments. Callee; this. _updateUITimerHandler = window.

SetTimeout(function() { func(); }, 1000); /* ...

This doesn't solve the need to access the appropriate instance members. Subsequent func() calls will have 'this' pointing at the window object instead of a specific instance of scheduler – AnthonyWJones Dec 10 '08 at 12:04.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions