473,799 Members | 2,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setTimeout() ignored in loop

I have a function called myFunction that can be called once from an
onclick() or 100 times from a function called loopFunction which
contains a for(var i=0;i<100;i++) loop.

Within myFunction is a setTimeout() function like this:

setTimeout(func tion(){shuffleD oor()},1500);

Now, when I call myFunction from onclick() it waits 1.5 seconds and
then calls the shuffleDoor function, just as I imagine it should.
However when I call the function from loopFunction it is called 100
times but there is no 1.5 second delay between calls.

Why is this?

Thanks in advance.
Jun 27 '08 #1
6 2268
Steve <st***********@ googlemail.comw rites:
I have a function called myFunction that can be called once from an
onclick() or 100 times from a function called loopFunction which
contains a for(var i=0;i<100;i++) loop.

Within myFunction is a setTimeout() function like this:

setTimeout(func tion(){shuffleD oor()},1500);

Now, when I call myFunction from onclick() it waits 1.5 seconds and
then calls the shuffleDoor function, just as I imagine it should.
However when I call the function from loopFunction it is called 100
times but there is no 1.5 second delay between calls.

Why is this?
Because you're doing it wrong. Your error is at line 15.

Whenever you suspect a bug in a javascript/host implementation (or any
other system that people have been using for years), it's your job to
show that the bug is really there. Since you haven't done that, I'm
going to assume the bug is really in your own code. Show the actual
code, and we can probably find the actual problem.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jun 27 '08 #2
Ugo
I have a function called myFunction that can be called once from an
onclick() or 100 times from a function called loopFunction which
contains a for(var i=0;i<100;i++) loop.

Within myFunction is a setTimeout() function like this:

setTimeout(func tion(){shuffleD oor()},1500);
it's preferable:
setTimeout( shuffleDoor, 1500 );
Now, when I call myFunction from onclick() it waits 1.5 seconds and
then calls the shuffleDoor function, just as I imagine it should.
ok
However when I call the function from loopFunction it is called 100
times but there is no 1.5 second delay between calls.
only the first one, I suppose
Why is this?
because the elapse time between single call of the loop is infinitesimal,
setTimeout does not stall the script, the script continues immediately (not
waiting for the timeout to expire); you schedule 100 contemporaneous
actions.
If you want execute the same function every 1,5 sec, the better way is to
use setInterval
Jun 27 '08 #3
On May 2, 9:29 pm, Ugo <priv...@nospam .itwrote:
because the elapse time between single call of the loop is infinitesimal,
setTimeout does not stall the script, the script continues immediately (not
waiting for the timeout to expire); you schedule 100 contemporaneous
actions.
If you want execute the same function every 1,5 sec, the better way is to
use setInterval
Thank you Ugo for a very clear and useful answer.

On May 2, 9:14 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
Because you're doing it wrong. Your error is at line 15.

Whenever you suspect a bug in a javascript/host implementation (or any
other system that people have been using for years), it's your job to
show that the bug is really there. Since you haven't done that, I'm
going to assume the bug is really in your own code. Show the actual
code, and we can probably find the actual problem.
This kind of smartass answer helps no-one. If you had bothered to read
the question you would see that I was asking about the behavior of
setTime() and not reporting a bug in javascript.

If you don't have an helpful answer why bother answering?
Jun 27 '08 #4
Steve <st***********@ googlemail.comw rites:
On May 2, 9:14 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
>Because you're doing it wrong. Your error is at line 15.

Whenever you suspect a bug in a javascript/host implementation (or any
other system that people have been using for years), it's your job to
show that the bug is really there. Since you haven't done that, I'm
going to assume the bug is really in your own code. Show the actual
code, and we can probably find the actual problem.

This kind of smartass answer helps no-one. If you had bothered to read
the question you would see that I was asking about the behavior of
setTime() and not reporting a bug in javascript.
I read the question. I just didn't notice that you actually did have a
delay - since you didn't mention that - and so I was assuming that you
had a real bug somewhere, instead of just the expected behaviour - as
Ugo pointed out.
If you don't have an helpful answer why bother answering?
I did provide a helpful reply: without a good description of the
problem and the relevant code, we're all just guessing.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jun 27 '08 #5
Ugo wrote:
>setTimeout(fun ction(){shuffle Door()},1500);

it's preferable:
setTimeout( shuffleDoor, 1500 );
*Maybe*, and if, then only in this case. For example, you really do *not*
want to write

window.setTimeo ut(foo.bar, 1500);

even if foo.bar does not have named arguments. shuffleDoor() could be the
method of an object in the scope chain as well (you did not quote enough to
assess that without further hassle).
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jun 27 '08 #6
Steve meinte:
I have a function called myFunction that can be called once from an
onclick() or 100 times from a function called loopFunction which
contains a for(var i=0;i<100;i++) loop.

Within myFunction is a setTimeout() function like this:

setTimeout(func tion(){shuffleD oor()},1500);

Now, when I call myFunction from onclick() it waits 1.5 seconds and
then calls the shuffleDoor function, just as I imagine it should.
However when I call the function from loopFunction it is called 100
times but there is no 1.5 second delay between calls.

Why is this?
As Ugo has already explained. Maybe window.setInter val() with a branch
for cancelling in the callback function is an option.

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jun 27 '08 #7

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

Similar topics

10
3073
by: Jupiter49 | last post by:
I'm trying to move pictures around on the screen, with a slight delay between the first picture and second picture (following onmousemove). When I add the setTimeout function I must be doing it wrong because the function that calculates the new coordinates for the picture returns 32 million and change instead of the coordinate. This causes the picture to not be able to appear on the display, obviously. Here's the calling code (snipped...
4
2949
by: Mool | last post by:
I want to use a setTimeout to just loop for a number of seconds, then fall through to the next bit of code...can this be done by using some kind of null in the statement, or do I have to assign the code a name and call it from the setTimeout once it's done? Thanks so much!
18
2208
by: Frances Del Rio | last post by:
this code is supposed to flip imgs at intervals of one second, but it flipps them much faster, it seems it flips them all in that first second, the more seconds the faster it flips them, instead of the other way around... (I mean it seems to me the more seconds I put in there the more seconds should go by betw. flips... each photo is in a div, each div has a z-index of 0, 1, 2, etc..) http://www.francesdelrio.com/cassini (pls reload a...
3
8596
by: jshanman | last post by:
I've created a "Play" button for my timeline that uses the Google Maps API. It basicly scrolls the timeline x interval every y milliseconds. The problem is the browser responds slowly even after the "Stop" button is clicked. (it responds slowly while playing, but thats to be expected with everything that it is doing) How can I get the browser back to it's more responsive state after the timeout is cleared? Here is the function that...
1
1722
by: Joris Lambrecht | last post by:
Hi people, Please take a look at the issue i talk about below. (yes i do realise such functions are publicaly available) The page i'm using uses JSON defined array of image files with information for the title property of the img tag. The functions i wrote below are sort of working since it needs an alert() to work the slides ???
18
9823
by: barry | last post by:
I'm having a problem creating a delay before each list item gets moved ( http://www.polisource.com/PublicMisc/list-splitter-timeout-bug.html ). Choose any number of columns, vertical layout, and view as a web page. They'll be a small delay before the entire bunch of list items converts from left-to-right to top-to-bottom order. I expected a delay before each item is moved. It looks like I have to flush the buffer to get setTimeout to work,...
6
1756
by: daveyand | last post by:
Hey Guys, I've stumped. I created a function that does various things to select boxes. Namely Get All selected indexes, populate array with these values
4
5226
by: si4 | last post by:
Hello, I'm currently studying javascript by myself. I thought that I will write js that will remove element in the body from DOM every 2 seconds, untill all elements in the body are deleted( except debug <span> :) ) At first I tried to go through the elements with loop, but when I call setTimeout() the loop doesn't wait setTimeout() to end, but it continues looping. Now I use recursion. setTimeout() call the function which do the...
18
15930
by: Csaba Gabor | last post by:
Is there a straightforward way of implementing a PHP equivalent to window.setTimeout? In a javascript web app, it's often the case that things are done on an event driven basis. For example, html elements might have things like onclick="buttonClicked()" to indicate that the function buttonClicked should be run when that element is clicked. The analogue can be done in PHP (on Windows) using com_event_sink. In other words, you can...
0
9541
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,...
0
10485
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10252
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10231
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
6805
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
5463
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...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
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 we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.