I want to set a hotkey to several functions by jquery hotkeys. And I need to check if a function is finished, may be something like:
if("function A is pleted")
{
"Ctrl+A is now set to function B"
}
else
{
"Ctrl+A is set to function A"
}
How could I check this? Or any better ideas?
I want to set a hotkey to several functions by jquery hotkeys. And I need to check if a function is finished, may be something like:
if("function A is pleted")
{
"Ctrl+A is now set to function B"
}
else
{
"Ctrl+A is set to function A"
}
How could I check this? Or any better ideas?
Share Improve this question edited Nov 20, 2014 at 23:34 BradleyDotNET 61.4k10 gold badges105 silver badges124 bronze badges asked Mar 11, 2014 at 11:09 LewisLewis 14.9k14 gold badges70 silver badges88 bronze badges2 Answers
Reset to default 2JavaScript on web browsers is single-threaded (barring the use of web workers, and even with those a function can't be interrupted), so except for a couple of bugs in issues with Firefox, a function cannot be interrupted in the middle. So if your code is running, the other function is not, by definition.
(For details on the issues with Firefox, see this answer by bobince about some very edge-case scenarios.)
Depending on the situation there are a couple of things you can do. You can call the function when you've finished pleting the current function or you can set a boolean for the same thing.
function A(){
alert('run items in function A');
return fireNewFunctionWhenComplete()
}
or set a flag
/*global var*/
var functionBCompleted = false;
function B(){
alert('run items in function B');
return functionBCompleted = true;
}
function testFunctionComplete(){
if(functionBCompleted){
alert('function B Copmleted');
}
else{
alert('function B not run');
}
}
This is a very simple example and as @T.J. mentioned you can't interrupt a running process. However, you may want to take a look into the promises spec if you want to run something when an asynchronous operation has pleted.