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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exiting a loop based on two different conditions

    I'm checking a variable to see if it holds a specific value. If it doesn't hold the value, I need to wait a few ms, and then check again. If at any time the var holds the correct value, I need to exit the loop and do something else with that var. I also want to exit the loop if the var test is false more than 5 times, at this point I would like to do something else (error message).

    I figured out how to loop until the var holds the right value, and I can loop for X amount of times, but I'm having trouble doing both at the same time.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,111
    Thanks
    75
    Thanked 4,333 Times in 4,299 Posts
    You can't just "wait a few ms" inside of a loop in JS coding.

    You have to use setTimeout to then execute some code after a number of milliseconds. But when you make the call to setTimeout( ), it returns to the caller *immediately* without waiting.

    Try to show some code and we'll see if we can find a different way to do what you are attempting.

    And by the way, in JS code if you are in a tight loop, then no other code *can* set that variable. So in any case you have to restructure the code.

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So setTimeout('doNothing()', 300); means the script will wait 300ms to execute doNothing(), while immediately continue with the rest of the loop, essentially ending the loop with 5 iterations of doNothing() executing in sequence?


    Mmmm, that would put a kink in this, as well as fully explain some of the behavior I was noticing before.

    In that case I'm going to have to restructure the whole function, 'cause that's not going to work for me at all.

    *time passes*

    Ok, I switched things around and got everything to work OK. I did some testing and found out that the resource I was waiting for never takes longer than 300ms, so I set my master function to run 1000ms later, and everything is good. It's not a big deal if this particular script is cobbled together in a hackish way, only 4 people will be using it.


    For curiosities sake, is the core of my question possible (2 conditions to exit a loop)? I don't think I need functioning code in verbatim, but if anyone could point me in the right direction I think I can figure it out.

    Thanks for the reply and tips Old Pedant.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,111
    Thanks
    75
    Thanked 4,333 Times in 4,299 Posts
    So setTimeout('doNothing()', 300); means the script will wait 300ms to execute doNothing(), while immediately continue with the rest of the loop, essentially ending the loop with 5 iterations of doNothing() executing in sequence?
    Exactly. 300 milliseconds after the loop ends (give or take).

    You can always exit a loop at any time using the break keyword:

    Silly example:
    Code:
    var iter = 0;
    while ( iter++ < 10 )
    {
        var r = Math.floor( Math.random() * 1000 ) + 1;
        if ( r > 950 ) break;
        if ( prompt("Is " + r + " your number?" ) break;
    }

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    This is really rough, and JS timers are tricky, but see if it helps.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>untitled</title>
    <style type="text/css">
    body {font:12px monospace;margin:40px;}
    </style>
    <script type = "text/javascript">
    
    function poller (polled, val, interval, attempts, action, msg)
    {
       var timer = setInterval(function()
                           {
                             if (eval(x) == val)
                             {
                               clearInterval(timer);
                               action('x=2');
                             }
                             else attempts--;
                             document.getElementById('readout').value = attempts; //demo, remove
                             if (attempts == 0)
                             {
                               clearInterval(timer);
                               alert(msg);
                             }
                           }, interval
                             );
    }
    
    function success(x)
    {
       alert(x);
    }
    
    var x = 0;
    
    function pstart(box)
    {
       box.checked = false;
       box.style.visibility = 'hidden';
       document.getElementsByTagName('input')[1].checked = false;
    
       poller(
              'x',    //variable checked
               2,     //good value
              100,    //interval ms.
              100,     //tries
              success,   //handler
             'Error!' //error msg
           );
    }
    
    </script>
    </head>
    <body>
    &raquo;&raquo; var x=0<br /><br />
    &raquo;&raquo; waiting for x to equal 2<br /><br />
    &raquo;&raquo; onclick="x=1" <input name="variable" type="radio" onclick="x=1" /><br />
    &raquo;&raquo; onclick="x=2" <input name="variable" type="radio" onclick="x=2" /><br />
    &raquo;&raquo; onclick="x=3" <input name="variable" type="radio" onclick="x=3" /><br /><br />
    &raquo;&raquo; remaining reads <input id="readout" type="text" style="border:none;" /><br /><br />
    &raquo;&raquo; start <input type="checkbox" onclick="pstart(this)" /><br /><br />
    <hr />
    <script type = "text/javascript">
    document.write(
                    '<pre>' ,
                    poller  ,
                    '</pre>'
                  );
    </script>
    </body>
    </html>
    Click 'def' to set the desired value, or wait it out ...
    Last edited by adios; 05-15-2009 at 05:56 AM.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,111
    Thanks
    75
    Thanked 4,333 Times in 4,299 Posts
    Looks pretty good, Adios. I would have just created the function non-anonymously, and I'd probably have used setTimeout so I didn't need to bother with clearing the interval, but those are just details.


  •  

    Posting Permissions

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