By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,376 Members | 1,691 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,376 IT Pros & Developers. It's quick & easy.

setTimeout() ignored in loop

P: n/a
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(function(){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.
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
Share this Question
Share on Google+
6 Replies


P: n/a
Steve <st***********@googlemail.comwrites:
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(function(){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.
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

P: n/a
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(function(){shuffleDoor()},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

P: n/a
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.nlwrote:
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

P: n/a
Steve <st***********@googlemail.comwrites:
On May 2, 9:14 pm, Joost Diepenmaat <jo...@zeekat.nlwrote:
>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

P: n/a
Ugo wrote:
>setTimeout(function(){shuffleDoor()},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.setTimeout(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

P: n/a
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(function(){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.
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.setInterval() 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 discussion thread is closed

Replies have been disabled for this discussion.