> 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 contem****aneous
actions.
If you want execute the same function every 1,5 sec, the better way is to
use setInterval


|