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

A

Antoine

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

Grant Wagner

Antoine said:
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.
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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top