1

What I want is:

Call a function check_and_wait() that tests whether a condition is true or false. It will keep checking/looping at set intervals for a specified amount of time.

The rest of the program must wait for the result of this test. This is the part that goes wrong. It does not have to use setInterval,that's just how it's implemented now.

function test_condition(){
    // do some test. return true or false
}

function check_and_wait(){
    var maxtime = 3000;
    var  start = new Date().getTime();
    condition = false;
    var interval = setInterval(function() {
        if(condition){return true;}
        else{
            if(  new Date().getTime() - start < maxtime ){
               condition = test_condition();
            }
            else{return false;}
        }
    }, 250); 

}

result = check_and_wait();
if(result){
    //do something
}
else{
     //do something else
}
2

3 Answers 3

2

How about using callbacks, it's so much easier to work with async events:

function check(finishCallback, waitingCallback) {
    var maxtime = 3000;
    var start = new Date().getTime();
    condition = false;
    var interval = setInterval(function() {
        if (condition) { 
            finishCallback(); 
        }
        else {
            if (new Date().getTime() - start < maxtime ) {
                condition = test_condition();
            } else {
                waitingCallback(); 
            }
        }
    }, 250); 
}

and then:

check(function() {
    //do something
}, function() {
    //do something else
});
Sign up to request clarification or add additional context in comments.

3 Comments

I was hoping to avoid this nesting code: functions nested inside functions inside functions. What if I again have to wait for something? Does all the rest of my code now need to go inside the check() function? So if I need to wait for something else then that will be another layer of nesting?
You cannot wait in javascript. This doesn't make sense. If you have lots of code (what you are calling rest of the code) simply put it in a function and then call this function from the appropriate callback. You should get accustomed to functional programming with callbacks if you want to do asynchronous javascript (things like AJAX, setInterval, ...). The sooner you get accustomed to this new coding styles, the less suffering you will have to endure :-)
I knew I hated javascript; I just hadn't realized how much! I'm stuck coding PhantomJS using js. Thanks for your answer.
1

When you deal with asynchronous processes you should consider using callback functions. For example in your case it could be:

function check_and_wait(callback) {
    var maxtime = 3000;
    var  start = new Date().getTime();
    var interval = setInterval(function() {
        if (new Date().getTime() - start < maxtime ){
            if (test_condition()) {
                clearInterval(interval);
                callback();
            }
        }
    }, 250);
}

check_and_wait(function() {
    // do something
});

Comments

1

You can use a callback will break out when the condition changes:

var interval;
function check_and_wait(){
    var maxtime = 3000;
    var  start = new Date().getTime();
    condition = false;
    interval = setInterval(function() {
        if(!condition)
            if(  new Date().getTime() - start < maxtime ){
               condition = test_condition();
            }
        }
        else {
           callback();
        }
    }, 250); 
}

function callback() {
     clearInterval(interval);
     // do the stuff you do when you're ready
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.