HighDots Forums  

List Running Timers in JavaScript (provided in html dom?)

Javascript JavaScript language (comp.lang.javascript)


Discuss List Running Timers in JavaScript (provided in html dom?) in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Antoine
 
Posts: n/a

Default List Running Timers in JavaScript (provided in html dom?) - 09-23-2004 , 05:02 PM






Hello,

Does anybody know a way to retreive the running timers (their ids) in a html
page?
I cannot find something in the html dom at first sight.
Is there collection of timers available (like window.all[], forms[],
frames[] and such)?
Some other way?

When you create a timer (setTimeout), the return value is the timer id.
Suppose that some script (which you do not own and cannot change) calls
setTimeout but doesn't store the id.
How to retreive it, to stop the timer (clearTimeout(id))?

I though to override the "setTimeout" like below, but that doesn't work.
Probably since the original setTimeout is used before I declare this new
one.

var oldSetTimeout=setTimeout;
var timerSaved=0;
setTimeout = new function(arg,time)
{
/*alert('timer: '+arg+', '+time);*/
timerSaved=oldSetTimeout(arg,time);
return timerSaved;
}

Greets,
P.



Reply With Quote
  #2  
Old   
Grant Wagner
 
Posts: n/a

Default Re: List Running Timers in JavaScript (provided in html dom?) - 09-28-2004 , 10:56 AM






Antoine wrote:

Quote:
Hello,

Does anybody know a way to retreive the running timers (their ids) in a html
page?
I cannot find something in the html dom at first sight.
Is there collection of timers available (like window.all[], forms[],
frames[] and such)?
Some other way?
No, there is no global collection of timer ids.

Quote:
When you create a timer (setTimeout), the return value is the timer id.
Suppose that some script (which you do not own and cannot change) calls
setTimeout but doesn't store the id.
How to retreive it, to stop the timer (clearTimeout(id))?
You don't. If you don't have a handle (id) for the setTimeout() setInterval()
you can't stop it.

Quote:
I though to override the "setTimeout" like below, but that doesn't work.
Probably since the original setTimeout is used before I declare this new
one.

var oldSetTimeout=setTimeout;
var timerSaved=0;
setTimeout = new function(arg,time)
{
/*alert('timer: '+arg+', '+time);*/
timerSaved=oldSetTimeout(arg,time);
return timerSaved;
}
It will work if you drop the "new" keyword:

var origSetTimeout = window.setTimeout;
window.setTimeout = function(f, t) {
alert('here');
return origSetTimeout(f, t);
}
var t = setTimeout('test()', 2000);
alert(t);
function test() { var x = 1; }

But I'm not sure what you hope to gain from this. You are saying that the
original script does not store the return value from setTimeout(). If that is
the case, overriding setTimeout() to return the value is useless (since it never
gets stored by the original script). Perhaps what you want to do is store the
timer id in a global collection for later retrieval by your own script:

window.setTimeout = function(f, t) {
if (!window.timerIds || window.timerIds.constructor != Array) {
window.timerIds = [];
}
window.timerIds.push(origSetTimeout(f, t));
}
setTimeout('test()', 2000);
setTimeout('test()', 2000);
setTimeout('test()', 2000);
alert(window.timerIds.join(';'));
function test() { var x = 1; }

You'll want to override clearTimeout() as well, to remove the Array element when
it's been cleared... probably. I don't know exactly what you're hoping to
achieve manipulating timers with script on a page that you can't change the
original script on.

--
Grant Wagner <gwagner (AT) agricoreunited (DOT) com>
comp.lang.javascript FAQ - http://jibbering.com/faq



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.