473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

reading props of a setTimeout

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

Dec 22 '05 #1
5 1476
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>
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com

Dec 23 '05 #2
Lee escreveu:
Jonas Raoni said:
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.


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
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com

Dec 23 '05 #3
On 23/12/2005 14:02, Jonas Raoni wrote:
Lee escreveu:
Jonas Raoni said:
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);
};
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.

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Dec 23 '05 #4
JRS: In article <43***********************@news.wanadoo.nl>, dated Thu,
22 Dec 2005 23:58:43 local, seen in news:comp.lang.javascript, Ivo
<no@thank.you> posted :
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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Dec 23 '05 #5
Lee escreveu:
Jonas Raoni said:
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?


Sorry, I was a bit crazy when I answered =]
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com

Dec 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Mic | last post by:
Goal: delay execution of form submit Code (Javascript + JScript ASP): <% Response.Write("<OBJECT ID='IntraLaunch' STYLE='display : none' WIDTH=0 HEIGHT=0...
4
by: Ken | last post by:
Hello I am trying to change the color of a font in a text box. I have been reading about and trying various examples found it this group, but still can't get it right. Here is where I am...
12
by: Arno R | last post by:
Hi there, I just distributed an application in which I (try to) change db.properties depending on CurrentUser() For instance I set the property's AllowBypassKey and AllowBuiltinToolbars to False...
13
by: Arno R | last post by:
Hi all In another thread I had problems changing db.props Another problem I encountered while testing this: db corrupt When I deleted db.props through code in a loop I could not start the db again...
4
by: Steve Drake | last post by:
All, If you right click on a word doc, you can see and set the custom props for the document, how can this be done in C# (or any other lang) I don't want to automate word as this will be...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
1
by: Chris Moltisanti | last post by:
Hey, I am using the setTimeout() function in IE to pause the execution of another funciton for a few seconds. My function is as follows: funtion test(e) { // the parameter 'e' is the event...
6
by: John | last post by:
Hi I am trying to save settings of controls on my form to a file so I can read them back later and recreate the controls on the form. I have figured out how to go through all controls and get...
1
by: bizt | last post by:
Hi, I have a process where every so often I refresh a table's data using a setTimeout() .. and when its no longer needed, a clearTimeout(). The following is a simple example of how this is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.