简体   繁体   中英

Call function again only after it finish executing

I have a function that gets called every time the enter key is pressed. I need to make it so that the function will NOT be called if you press the enter key again, IF and when the function has not finish executing yet. Only when the function has finished executing, will it be called again if you press the enter key again. How would I do this in jQuery, or JavaScript?

onKeyDown = 'if(event.keyCode==13) someFunction()'

function someFunction() {

//some codes
//some setTimeouts

}

Simply set a Boolean when done and check it before executing the function.

 onKeyDown = 'if(event.keyCode==13) { if (!finished){someFunction();}' var finished = false; function someFunction() { finished = false //some codes //some setTimeouts finished = true; } 

Though, as Luis points out, Javascript is single threaded and so you shouldn't be running into the problems you describe unless I am not understanding your problem fully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM