...

View Full Version : Resolved Help with setting a delay for a loop (setTimeout isn't helping)



oliverb4ss
10-08-2010, 02:27 PM
I have a while-loop and I need it to make a little delay before each time it starts a new cycle.



var rollOn = true;
while(rollOn)
{
//A bunch of statements.
//The loop breaks in case of certain conditions so that "rollOn = false;".
var t = setTimeout("continue", 1000);
} //End while

The problem is that setTimeout doesn't delay for a second before the "continue" statement. For example, when I write:


var t = setTimeout("alert("Delay")", 1000);
then it displays the alert box after every cycle of the loop, but still no delay.

There are no error messages or anything, I'm not really sure what's the problem, thus I don't know what to do about it.

Any ideas?

DaveyErwin
10-08-2010, 02:49 PM
<div id="display">x</div>


<script>
var display = document.getElementById('display')
var loopWithPause = function(count){
count--;
display.innerHTML += 'x';
if(count){
setTimeout('loopWithPause('+count+')',1000);
}
}
loopWithPause(10)
</script>

oliverb4ss
10-08-2010, 03:18 PM
Thanks, I'm pretty sure I understand that snippet of code well enough to use it, tho I'm not sure what '+count+' means (It isn't the same as 'count++', is it? That wouldn't make sense, it would create and infinite loop).

So, this is what I've got now:


var rollOn = true;
while(rollOn)
{
//A bunch of statements.
//The loop breaks in case of certain conditions so that "rollOn = false;".
var loopWithPause = function(count)
{
count--;
if(count)
{
setTimeout('loopWithPause('+count+')',1000);
}
}
loopWithPause(1)
} //End while


But there's still no delay and I'm not quite sure if I'm using this right considering there was no explanation for the code.

Philip M
10-08-2010, 03:39 PM
Try this:-


<div id="display"></div>

<script type = "text/javascript">
var count = 0;

function loopWithPause() {
var display = document.getElementById('display');
display.innerHTML += 'x';
count ++;
if (count < 5 ) { // or some other condition is fulfilled
setTimeout('loopWithPause()',1000); // one second delay
}
else {count = 0} // reset
}

setTimeout('loopWithPause()',1000);

</script>


Quizmaster: What variety of large white bear can be found in the Arctic region?
Contestant: Penguin.

DaveyErwin
10-08-2010, 03:41 PM
<div id="display">x</div>
<script>
var display = document.getElementById('display')
var rollOn = true;
var rollit = function(){
//A bunch of statements.
//The loop breaks in case of certain conditions so that "rollOn = false;".
if(display.innerHTML.length > 10){rollOn = false}
display.innerHTML += 'x';
if(rollOn){
setTimeout('rollit()',1000);
}

}

rollit();

</script>

DaveyErwin
10-08-2010, 03:42 PM
The poor kitty
it dies many deaths

oliverb4ss
10-08-2010, 04:26 PM
I've tried this every which way, using both of your example, but still nothing.

Can any of you guess why the setTimeout function doesn't work, for example the setTimeout("continue",1000); inside a loop?

It seems pretty straightforward: it waits for a second and then starts the next cycle of the loop. Am I missing something javascript-specific?

Philip M
10-08-2010, 04:54 PM
Both the scripts you have been given work fine for me. :)

The syntax is setTimeout("continue()",1000);
or
setTimeout(continue,1000); // no quotes

var t = setTimeout("alert("Delay")", 1000);
will not work as you have double quotes within double quotes.
Should be
var t = setTimeout("alert('Delay')", 1000);

If I were you I would use Firebug to check for syntax errors.

oliverb4ss
10-08-2010, 05:22 PM
With the exception of the aforementioned setTimeout() problem, the code (whole webpage) works perfectly with Chrome, Opera and Safari, but with Firefox it's buggy and Firebug displays my scripts page (http://web.zone.ee/side_laborid/pig/game_script.js) as random gibberish. So I Firebug isn't really helpful (also, I have no idea why Firefox behaves the way it does).

By the way, the while-loop requiring the delay is in the rollCPUDice() function.

I've been at this for days, I think it's getting to my sanity :)

Philip M
10-08-2010, 05:30 PM
Suggest you fix the errors in your markup first. See http://validator.w3.org/

If it does not work properly in Firefox, then the fault, dear Brutus, lies not in the stars ....... :)

oliverb4ss
10-08-2010, 05:41 PM
The page passes the validator with flying colors (well, just the green of approval, but still).

I'm going to try to do this with setInterval(), but not until tomorrow, I'll call it a night.

Philip M
10-08-2010, 05:46 PM
continue is a Javascript reserved word. Change it.

oliverb4ss
10-08-2010, 06:02 PM
I mean to use continue as the command to start the next cycle of the loop, as in:


while(true)
{
//do some stuff
//wait for a bit and then continue;
}

In my case, setTimeout(continue,1000); would wait one second and then start the next cycle.

oliverb4ss
10-08-2010, 06:16 PM
Never mind, I reworked it using the setInterval() function. I wrote the while-loop into a separate function rollOn(), which in itself had clearInterval() lines at the appropriate places, and then called it using setInterval("rollOn()",1000);.

Thanks anyway for taking the time to help me. Now I have to tackle the incompatibility with Firefox, but I think I've tracked it down to a relatively simple bug.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum