473,668 Members | 2,552 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 "clearTimeo ut" call does not do
anything special to handle things this way. All it does is the following:
"clearTimeout(t heTimeOut)", where the "theTimeOut " variable is just an
integer!

If you ask me, the "clearTimeout(t heTimeOut)" 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(t heTimeOut)" 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 2116
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.javas cript 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.comwrot e:
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******** ************@gi ganews.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.javas cript 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**********@g mail.comwrote in message
news:11******** **************@ y5g2000hsa.goog legroups.com...
Hi Rene,

On Apr 21, 10:05 am, "Rene" <a...@b.comwrot e:
>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
14929
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
4
8400
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 now: <script language="JavaScript">
2
4644
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, it should simply present an alert message after 5 seconds. Thanks. <html> <head> <script language="javascript"> function MsgTimer() { var self = this;
6
2243
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 = document.getElementById(divId); setTimeout(id.style.display = 'none',timeout); } Thanks for any help.
4
1928
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 also have this javascript code: ----------------------------------------
12
5539
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. Foo = function(type) { this.num = 0; this.type = type this.trigger(); } Foo.prototype.trigger = function() {
4
5226
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 javascript function which calls setTimeout and will process a second javascript function "Warn" just before the session expires. The Warn function displays an html page with a button. A second timer is started to cause the html page to close...
15
3780
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 image rotates. I cannot figure out how to accomplish the 3 second delay. My code is pasted below: function randPic(){ randPic1(); randPic2();
7
2405
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 interfer with one another.
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8367
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8570
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8650
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7391
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1779
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.