473,403 Members | 2,293 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,403 software developers and data experts.

JavaScript Semantics Question (setTimeout)


Consider the simple example:

http://www.w3schools.com/js/tryit.as...js_timing_stop

Note that if you press the start button N times, the counter
increments by 1, N times. So you must press the stop button N
times to stop the incrementing entirely.

The question is what is the semantics here that is responsible
for this stacking, for want of a better word.

Jul 28 '06 #1
2 1667
Ian A. Mason wrote:
Consider the simple example:

http://www.w3schools.com/js/tryit.as...js_timing_stop

Note that if you press the start button N times, the counter
increments by 1, N times. So you must press the stop button N
times to stop the incrementing entirely.

The question is what is the semantics here that is responsible
for this stacking, for want of a better word.
There is no 'stacking'. setTimeout() returns a reference (an integer)
that can be used by clearTimeout() to cancel the setTimeout every time
it runs.

In the w3schools example, that reference is stored in the global
variable 't', which is re-set every time setTimeout runs so it always
references the last setTimeout. Since the w3schools example just
increments a counter you can't distinguish which one is actually running
or which one you cancel.

Anyhow, pressing the 'Stop count!' button just clears the last
setTimeout that was called, the others (if there are any) will keep
running. When they next run, they reset t again and you can cancel
another one.

If you have a long interval, you can click 'stop count' several times
and not stop the other timeout because you won't get a reference to it
until it runs again (t referenced the canceled timeout, you have to wait
until it is reset so you can cancel one of the running setTimeouts).

To see it in action, change the line:

document.getElementById('txt').value=c
to

document.getElementById('txt').value=t
and you'll see the value of 't' changing.
--
Rob
Jul 28 '06 #2
>When they next run, they reset t again and you can cancel
another one.
Right, thats what I was overlooking! Thanks.
Jul 29 '06 #3

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

Similar topics

4
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a...
10
by: Shadow Lynx | last post by:
That subject packs a whallop, so let me explain in better detail what's happening and how it relates to ASPX pages... In a nutshell, if the first <script /on a page is of type "text/vbscript",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.