#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0

    Loop that kills time until a flag is set to true


    Does anybody know how to create a loop that will just kill time until a flag is set. Maybe something like:

    var flag =false;

    while ( flag == false ){
    //kill time then check if flag is true yet
    //If flag is still false kill some more time
    }

    Thanks, Mike
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,143
    Rep Power
    9398
    That is a very, very bad way of doing it. You'll tie up the browser until it crashes or, more likely, it decides to kill your script or even the whole tab/window.

    If you can execute code that sets the flag then you can also execute code that executes code. Make it call a function or something.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    22
    Rep Power
    0
    What you need is a subscribe design pattern.

    en.wikipedia.org/wiki/Publish–subscribe_pattern

    There is many way to implement this on Javscript, This is one way.


    Code:
    var subscription = [];  //set this on global level or under global function
    
    //you can add any function to subscription
    subscription.push(function(){
        alert("It is ready!");
    });
    
    subscription.push(function(){
        alert("It is really ready!");
    });
    
    
    
    //call this function when your event is ready
    function notify() {
       for (var item in subscription) {
           subscription(item)();
       }
    
       //if you want to clear the subscription after execution then uncomment the following line
        //subscription=[];
    }

IMN logo majestic logo threadwatch logo seochat tools logo