Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}
like image 463
jessica Avatar asked Jan 25 '26 13:01

jessica


1 Answers

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.

like image 159
Scott Avatar answered Jan 27 '26 02:01

Scott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!