I want to create a javascript 'wait' function. what should i edit ?
function wait(waitsecs){
setTimeout(donothing(), 'waitsecs');
}
function donothing() {
}
I want to create a javascript 'wait' function. what should i edit ?
function wait(waitsecs){
setTimeout(donothing(), 'waitsecs');
}
function donothing() {
}
Javascript isn't threaded, so a "wait" would freeze the entire page (and probably cause the browser to stop running the script entirely).
To specifically address your problem, you should remove the brackets after
donothing
in yoursetTimeout
call, and makewaitsecs
a number not a string:But that won't stop execution; "after" will be logged before your function runs.
To wait properly, you can use anonymous functions:
All your variables will still be there in the "after" section. You shouldn't chain these - if you find yourself needing to, you need to look at how you're structuring the program. Also you may want to use
setInterval
/clearInterval
if it needs to loop.You shouldn't edit it, you should completely scrap it.
Any attempt to make execution stop for a certain amount of time will lock up the browser and switch it to a Not Responding state. The only thing you can do is use
setTimeout
correctly.