473,508 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confused about setTimeout function.

Hi, first things first, please take a second to check out the page on the
link below.

http://bookofjavascript.com/Chapter13/Assignment.html

In this page, you are supposed to click on the "Make Happiness Bounce"
button to make the smiley face start bouncing and then you can click the
"Stop that Smiley!" button to stop it.

What I find odd about this example is that if I click the "Make Happiness
Bounce" button 5 times the bouncing speeds up and then if I click on the
"Stop that Smiley!" button 5 times the bouncing slow down and stop! I think
I know why it speeds up but what I don't understand is why it slows down.

How is this happening? According to me, the "clearTimeout" call does not do
anything special to handle things this way. All it does is the following:
"clearTimeout(theTimeOut)", where the "theTimeOut" variable is just an
integer!

If you ask me, the "clearTimeout(theTimeOut)" call should *only* be able
stop the *last* "setTimeout" call but it shouldn't be able to stop previous
calls to "setTimeout" calls

The reason I say this is because previous references to "setTimeout" calls
where stored on the "theTimeOut" variable but this variable is overridden
every time a new call to the "setTimeout" occurs. So how can the
"clearTimeout(theTimeOut)" know the history of the values that were stored
on the "theTimeOut" variable to be able to stop previous "setTimeout" calls?

I am really confused with this behavior, any help explaining the reason for
this behavior is greatly appreciated.

Thanks you.
Apr 21 '07 #1
4 2096
Rene said the following on 4/21/2007 1:05 PM:
Hi, first things first, please take a second to check out the page on the
link below.

http://bookofjavascript.com/Chapter13/Assignment.html
Personally, I wouldn't speed up and slow down an animation they way that
page does. Rather than using multiple timeouts you simply change the
time on the timeout. Instead of calling two timeouts, you change the
time from, say, 100ms to 90ms to "speed it up". And then to "slow it
down", you change it from 90 to 100. That would allow a third button
that was a true "Stop Smiley" because you would only have one timeout to
clear out.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 21 '07 #2
Hi Rene,

On Apr 21, 10:05 am, "Rene" <a...@b.comwrote:
Hi, first things first, please take a second to check out the page on the
link below.

http://bookofjavascript.com/Chapter13/Assignment.html

In this page, you are supposed to click on the "Make Happiness Bounce"
button to make the smiley face start bouncing and then you can click the
"Stop that Smiley!" button to stop it.

What I find odd about this example is that if I click the "Make Happiness
Bounce" button 5 times the bouncing speeds up and then if I click on the
"Stop that Smiley!" button 5 times the bouncing slow down and stop! I think
I know why it speeds up but what I don't understand is why it slows down.

How is this happening?
This took me some puzzling also. Very strange scripting but
interesting and it does make sense.

When you press start the first time you set up a recursive call to
move(). Every time move() runs it resets theTimeOut.

When you press start the second time you set up another recursive call
to move(). Every time this loop of move() calls runs it also resets
theTimeOut.

When you press stop the first time you stop one of the above two
loops...not necessarily the second loop! You stop the loop that last
set theTimeOut. If you could press the stop button really quickly a
second time then what you are expecting would happen: the other loop
would keep running because the integer stored in theTimeOut hasn't
changed.

So after the first stop click, the other loop of move() executions
keeps running and keeps resetting theTimeOut to new integer values.
When you click the stop button after this has happened then this other
loop stops also and the image stops moving.

The key issue here is theTimeOut is being reset many, many times and
not just twice. Perhaps you were thinking it was set just twice like
it would be if this solution was created with setInterval(). This
thinking caught me for a moment.

I hope that helps.

Peter

Apr 21 '07 #3
Thanks Randy, but what I really wanted to know is why the example is
actually working!

Like I said on my original post, the code should *only* be able to clear the
*last* "setTimeout", how is it possible that the code is able to clear
previous "setTimeout" calls?? What's going on behind the scenes??

Thank you.





"Randy Webb" <Hi************@aol.comwrote in message
news:sa********************@giganews.com...
Rene said the following on 4/21/2007 1:05 PM:
>Hi, first things first, please take a second to check out the page on the
link below.

http://bookofjavascript.com/Chapter13/Assignment.html

Personally, I wouldn't speed up and slow down an animation they way that
page does. Rather than using multiple timeouts you simply change the time
on the timeout. Instead of calling two timeouts, you change the time from,
say, 100ms to 90ms to "speed it up". And then to "slow it down", you
change it from 90 to 100. That would allow a third button that was a true
"Stop Smiley" because you would only have one timeout to clear out.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/

Apr 21 '07 #4
Tim
Aaaah, I am such an idiot, half way from reading your response and
everything started clicking! I can't believe I didn't catch that!

I can finally move on to chapter 14 in peace!

Thanks a lot!

"Peter Michaux" <pe**********@gmail.comwrote in message
news:11**********************@y5g2000hsa.googlegro ups.com...
Hi Rene,

On Apr 21, 10:05 am, "Rene" <a...@b.comwrote:
>Hi, first things first, please take a second to check out the page on the
link below.

http://bookofjavascript.com/Chapter13/Assignment.html

In this page, you are supposed to click on the "Make Happiness Bounce"
button to make the smiley face start bouncing and then you can click the
"Stop that Smiley!" button to stop it.

What I find odd about this example is that if I click the "Make Happiness
Bounce" button 5 times the bouncing speeds up and then if I click on the
"Stop that Smiley!" button 5 times the bouncing slow down and stop! I
think
I know why it speeds up but what I don't understand is why it slows down.

How is this happening?

This took me some puzzling also. Very strange scripting but
interesting and it does make sense.

When you press start the first time you set up a recursive call to
move(). Every time move() runs it resets theTimeOut.

When you press start the second time you set up another recursive call
to move(). Every time this loop of move() calls runs it also resets
theTimeOut.

When you press stop the first time you stop one of the above two
loops...not necessarily the second loop! You stop the loop that last
set theTimeOut. If you could press the stop button really quickly a
second time then what you are expecting would happen: the other loop
would keep running because the integer stored in theTimeOut hasn't
changed.

So after the first stop click, the other loop of move() executions
keeps running and keeps resetting theTimeOut to new integer values.
When you click the stop button after this has happened then this other
loop stops also and the image stops moving.

The key issue here is theTimeOut is being reset many, many times and
not just twice. Perhaps you were thinking it was set just twice like
it would be if this solution was created with setInterval(). This
thinking caught me for a moment.

I hope that helps.

Peter

Apr 21 '07 #5

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

Similar topics

3
14908
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
4
8378
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...
2
4631
by: Athanasius | last post by:
Could someone shed some light as to why the following setTimeout function will not work on the Mac IE5.2? It does however work on PC(Forefox,Netscape,IE) & Mac(Safari,Firefox). Here is the script,...
6
2231
by: Brent | last post by:
Is there any obvious reason why there's no delay in the execution of setting the id.style.display when this function gets called? function Hide(divId,timeout) { var id =...
4
1915
by: Telmo Costa | last post by:
I have a HTML page with a form with 2 buttons like this .... <input type="button" value="Add" onClick="ClickAdd();"/> <input type="button" value="Reset" onClick="ClickReset();"/> .... I...
12
5505
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. ...
4
5207
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...
15
3752
by: nikki_herring | last post by:
I am using setTimeout( ) to continuously call a function that randomly rotates/displays 2 images on a page. The part I need help with is the second image should rotate 3 seconds after the first...
7
2392
by: Martin | last post by:
Can I have two setTimeouts running at the same time - with two different intervals? I want to start one timer and, before it times out, start another one I've tried this and they seems to...
0
7132
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
7336
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
7401
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...
1
5059
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...
0
4720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.