jConfirm doesn't return true or false

402 Views Asked by At

Here is what I'm trying to do....

function myFunction()
{
  var x=jConfirm('Can you confirm this?', 'Confirmation Dialog', function (r) {
      y=r; 
      alert(y);
  });

  alert(x);
 }

Why does alert(x) runs first(I'm beginner in javascript) and it alerts "undefined", and after I press Ok it alerts "True", and "False" when I press Cancel..?

Is there any way I can make jConfirm return true or false...ie x to be trur or false?

I'm using the "http://abeautifulsite.net/" plugin for jConfirm.

1

There are 1 best solutions below

11
On

jConfirm doesn't create alert boxes, it creates a HTML element which looks like an alert box. The important difference here is that an alert() box will stop Javascript execution until it's closed, so alert(1); console.log(2); will alert first, then log to the console. Since jConfirm doesn't use alert(), if you call jConfirm(1, ...); console.log(2) it will show the confirm box (HTML), then write to the console a split-second later.

The complicated-words version of this is that alert() is synchronous - it locks the script execution until you've chosen something from the alert. jConfirm is asynchronous, and the script execution continues independently of your choice until you choose something, at which point it triggers a callback function.