reading props of a setTimeout

I

Ivo

Hi,
somewhere in my code a timeout is set in a function:

var timer = null;
function foo( n ) {
var delay = 500;
// do stuff with n
timer = window.setTimeout( 'foo(' + ( ++n ) + ');', delay );
}

Now elsewhere in another function, called during a totally unrelated event,
this time-out needs to be canceled. Not a problem:

if( timer ) { window.clearTimeout( timer ); }

But before erasing the values, I would like to store the string and delay
parameters used when setting the last timer, to be exact if I know the value
of n, I can at some time after the cancelling restart the timer at the point
it was canceled. Saving this information is of course possible in
traditional ways with more code in the function, more global variables, etc.
but is there a way to extract the info simply from the setTimeout where it
resides and which I am dealing with anyway? Such functionality could highly
simplify lots of work.

hth
ivo
 
J

Jonas Raoni

Ivo escreveu:
Saving this information is of course possible in
traditional ways with more code in the function, more global variables, etc.
but is there a way to extract the info simply from the setTimeout where it
resides and which I am dealing with anyway? Such functionality could highly
simplify lots of work.

There are a lot of ways to do it, bellow I've made an example without
using global variables, I hope it will help you...

Click on the document to play/pause the timer ;]

<script type="text/javascript">
Timer = function(n, interval){
this.n = n || 0;
this.interval = interval || 1000;
this.running = false;
this._timer = null;
};
Timer.prototype.ontimer = function(){
++this.n;
document.title = this.n;
};
Timer.prototype.run = function(){
function getHandler(instance){
return function(){
instance.ontimer();
};
}
!this._timer && (this._timer = setInterval(getHandler(this),
this.interval));
this.running = true;
};
Timer.prototype.stop = function(){
clearTimeout(this._timer), this._timer = null;
this.running = false;
};

var o = new Timer;
document.onclick = function(){
if(o.running)
o.stop(), alert("Stopped at " + o.n);
else
o.run(), alert("Started at " + o.n);
};
</script>
 
L

Lee

Jonas Raoni said:
Ivo escreveu:
Saving this information is of course possible in
traditional ways with more code in the function, more global variables, etc.
but is there a way to extract the info simply from the setTimeout where it
resides and which I am dealing with anyway? Such functionality could highly
simplify lots of work.

There are a lot of ways to do it, bellow I've made an example without
using global variables, I hope it will help you...

Click on the document to play/pause the timer ;]

<script type="text/javascript">
Timer = function(n, interval){
this.n = n || 0;
this.interval = interval || 1000;
this.running = false;
this._timer = null;
};
Timer.prototype.ontimer = function(){
++this.n;
document.title = this.n;
};
Timer.prototype.run = function(){
function getHandler(instance){
return function(){
instance.ontimer();
};
}
!this._timer && (this._timer = setInterval(getHandler(this),
this.interval));
this.running = true;
};
Timer.prototype.stop = function(){
clearTimeout(this._timer), this._timer = null;
this.running = false;
};

var o = new Timer;
document.onclick = function(){
if(o.running)
o.stop(), alert("Stopped at " + o.n);
else
o.run(), alert("Started at " + o.n);
};
</script>

Maybe I've missed something, but it looks to me as if "o" is a global variable.
 
J

Jonas Raoni

Lee escreveu:
Jonas Raoni said:
Maybe I've missed something, but it looks to me as if "o" is a global variable.

Awww, don't act like Mr. Spock, I'm just trying to help the other guy,
I'm not here to discuss superfluous things nor "fight"... =/

But you're really missed something, not just the "o" is a global var...
The Timer is one too... But you know everything can be hidden, it's
quite easy... But in my opinion it isn't worth to write such things:

(function(){
var o = new Timer;
document.onclick = function(){
o.running ? (o.stop(), alert("Stopped at " + o.n)) : (o.run(),
alert("Started at " + o.n));
};
})();

(document.onclick = function(){
var o = arguments.callee.o;
o.running ? (o.stop(), alert("Stopped at " + o.n)) : (o.run(),
alert("Started at " + o.n));
}).o = new Timer;

:

There are a bunch of ways to achieve this... I'll check if I can answer
another email, then I'll be away, time for the party... I hope Mr.
Spock to not reply my messages :D
 
M

Michael Winter

Lee escreveu:
Jonas Raoni said:


Maybe I've missed something, but it looks to me as if "o" is a
global variable.

Awww, don't act like Mr. Spock [...]

I think you misunderstand. Assuming Ivo is the same one I recall, he
would be quite capable of writing an object wrapper from which the
timeout data could be obtained later. I believe that what he was asking
for was a known 'getTimeoutData' function of some sort that, given a
timer id, would return the arguments passed when creating that timer.

I doubt that there is such a built-in function anywhere, but you could
roll your own data structure[1] to fulfil the same goal, rather than a
simple wrapper.
But you're really missed something, not just the "o" is a global var...
The Timer is one too... But you know everything can be hidden, it's
quite easy... But in my opinion it isn't worth to write such things:

I should think it depends on the type of code involved. If you intend
for it to be reused in some other context, it can be quite useful to
minimise the variables injected into the global object. Other times, as
you say, it isn't always worth the effort.

[snip]
(document.onclick = function(){
var o = arguments.callee.o;
o.running ? (o.stop(), alert("Stopped at " + o.n)) : (o.run(),
alert("Started at " + o.n));
}).o = new Timer;

An interesting example, but I would stick to the former. Not all user
agents (Opera, as I recall, is one) implement the callee property (which
is deprecated in JavaScript, and not defined by ECMA-262).

[snip]

Mike


[1] When I've bothered to look at the actual value returned by
setTimeout, it's always been a number. If this is /always/
true, then a simple object could be used instead of a map.
 
L

Lee

Jonas Raoni said:
Lee escreveu:

Awww, don't act like Mr. Spock, I'm just trying to help the other guy,
I'm not here to discuss superfluous things nor "fight"... =/

I don't want to fight, either, but in what way is my pointing out your
error more offensive than the attitude you've chosen to take?
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Thu,
22 Dec 2005 23:58:43 local, seen in Ivo
Hi,
somewhere in my code a timeout is set in a function:

var timer = null;
function foo( n ) {
var delay = 500;
// do stuff with n
timer = window.setTimeout( 'foo(' + ( ++n ) + ');', delay );
}

Now elsewhere in another function, called during a totally unrelated event,
this time-out needs to be canceled. Not a problem:

if( timer ) { window.clearTimeout( timer ); }

But before erasing the values, I would like to store the string and delay
parameters used when setting the last timer, to be exact if I know the value
of n, I can at some time after the cancelling restart the timer at the point
it was canceled. Saving this information is of course possible in
traditional ways with more code in the function, more global variables, etc.
but is there a way to extract the info simply from the setTimeout where it
resides and which I am dealing with anyway? Such functionality could highly
simplify lots of work.

AFAIK not.

But, for a long timeout, you could break the delay into individual
slices - setInterval - keep track of the number of intervals, and use
that to stop when finished and to restart when interrupted.

The string and initial delay can be saved by enclosing the initial set
in a wrapper.
 
J

Jonas Raoni

Lee escreveu:
Jonas Raoni said:

I don't want to fight, either, but in what way is my pointing out your
error more offensive than the attitude you've chosen to take?

Sorry, I was a bit crazy when I answered =]
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top