Check if function can be called: Javascript -
i'm trying write javascript function calls dosomething() continuously, long some_condition true. if some_condition becomes false, should check after 5 seconds whether some_condition has again become true, , if not, display error message on screen.
i have of function written, confused whether doing correctly. help.
while (some_condition) { dosomething(); else { ??? }
no, should not use while
loop. javascript single threaded, , if have while
loop never returns, browser (or javascript environment of choice) become unresponsive.
you need use settimeout
schedule function.
function enqueuesomething() { if (some_condition) dosomething(); // enqueue ourselves run again without blocking thread of execution settimeout(enqueuesomething); else // wait 5 seconds, try again settimeout(enqueuesomething, 5000); }
you'll need maintain state indicates whether you've waiting 5 seconds, , add error-alerting code.
Comments
Post a Comment