Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Location
    Tallinn, Estonia
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with setting a delay for a loop (setTimeout isn't helping)

    I have a while-loop and I need it to make a little delay before each time it starts a new cycle.

    Code:
    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:
    Code:
    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?
    Last edited by oliverb4ss; 10-08-2010 at 06:17 PM.

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Code:
    <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>

  • #3
    New to the CF scene
    Join Date
    Oct 2010
    Location
    Tallinn, Estonia
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    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.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Try this:-

    Code:
    <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.

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Code:
    <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>

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    The poor kitty
    it dies many deaths

  • #7
    New to the CF scene
    Join Date
    Oct 2010
    Location
    Tallinn, Estonia
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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.
    Last edited by Philip M; 10-08-2010 at 05:01 PM.

  • #9
    New to the CF scene
    Join Date
    Oct 2010
    Location
    Tallinn, Estonia
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 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

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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 .......

  • #11
    New to the CF scene
    Join Date
    Oct 2010
    Location
    Tallinn, Estonia
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    continue is a Javascript reserved word. Change it.

  • #13
    New to the CF scene
    Join Date
    Oct 2010
    Location
    Tallinn, Estonia
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I mean to use continue as the command to start the next cycle of the loop, as in:
    Code:
    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.

  • #14
    New to the CF scene
    Join Date
    Oct 2010
    Location
    Tallinn, Estonia
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •