Reputation: 13
I am showing a Delete confirmation dialogue box, and I want the result of User's click of YES or NO as TRUE or FALSE
Now, since Javascript function is asynchronous, the variable yesDelete in the code below does not wait for the users choice of confirmation.
Now, is there a way to return the value r as the returnvalue of the function ShowDeleteConfirm()?
function ShowDeleteConfirm() {
var yesDelete=false;
jConfirm('Can you confirm this?', 'Confirmation Dialog', function (r) {
yesDelete = r;
});
return yesDelete;
}
Upvotes: 1
Views: 1312
Reputation: 3836
One way to do it would be to pass a callback function as an argument either to ShowDeleteConfirm()
or to jConfirm()
. Something like this:
function ShowDeleteConfirm(callback) {
//someone correct me if I'm out of scope here...
jConfirm('Can you confirm this?', 'Confirmation Dialog', function (r) {
callback(r);
});
}
I think that will work, but I'm not sure if callback
is in the scope of jConfirm's callback. Worth a shot though :)
The otherway to do it would be to have the jConfirm callback function set a property on the window
object where it's globally visible.
Anyway, hope that helps a little at least...
EDIT
Because JS is asynchronous, I'm don't think you can have ShowDeleteConfirm()
return true
or false
based on the users input. Hence the need to pass in a different callback function: ShowDeleteConfirm(yourCallbackFunctionHere)
. You could do this:
ShowDeleteConfirm(function(result) {
//result should be true or false
});
but I think that's as close as you're going to get. If that doesn't work for you, then you should probably think about changing your design to accommodate the fact that JS is asynchronous.
Good luck!
Upvotes: 1
Reputation: 6122
I think following will be ur case:
if (ShowDeleteConfirm()){
myFunc();
}
function ShowDeleteConfirm() {
var yesDelete=false;
jConfirm('Can you confirm this?', 'Confirmation Dialog', function (r) {
yesDelete = r;
});
return yesDelete;
}
should be like this:
ShowDeleteConfirm(myFunc);
function ShowDeleteConfirm(myFunc) {
jConfirm('Can you confirm this?', 'Confirmation Dialog', function (r) {
if (r){
myFunc();
}
});
}
Upvotes: 0