Is it possible to have the return of a JS function depend on user action?
So let’s say I have function a and function b.
Function b brings up a dialog with two buttons, one which returns true and one which returns false. Can I call b from a and force a to wait for the return from b before continuing it’s operation?
Example code:
function a(){
if(b()){
do x
}else{
do y
}
}
function b(){
$("#anElement").dialog({
title: "This is a Dialog",
buttons:{
"Yes": function(){
//do some stuff
$(this).dialog("close");
return true;
},
Cancel: function(){
//do some stuff
$(this).dialog("close");
return false;
}
}
});
}
I guess this is a lot like a confirm, but I want to build more than just yes / no on top of it. Any suggestions?
No, you can’t halt a function’s execution (well, you could with an infinite while loop with a break condition, but that’s terrible practice). However, you could split A into two functions and call the other half of function A from function B. You could even pass in a return function to function B and call that, passing in the value of the user action.
Example: