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
}