1

I am using jquery-confirm (https://craftpip.github.io/jquery-confirm/) to confirm a cancellation on a form, but it is not working. My function is not returning the result of the jquery confirmation:

function cnf(cntn, fn, prm) {
    var res = false;
    $.confirm({
        title: "",
        content: cntn,
        confirmButton: "Yes",
        cancelButton: "No",
        confirm: function() { res = true; fn(prm); },
        cancel: function() { }
    });
    return res;
}
function emptyfn() { }
function link() {
    return cnf("Cancel changes?", emptyfn);
}

...

<form method="post" action="whatever.php">
    <label for="test">Test Input</label>
    <input type="text" class="form-control" id="test" maxlength="40" name="test" autofocus>
    <input type="submit" name="button" value="Submit" class="btn-primary">
    <input type="submit" name="button" value="Cancel" class="btn-primary" onclick="return link();" formnovalidate>
</form>

(I know I don't need "fn" and "prm" in this example, but I am trying to generalise the code for use elsewhere).

When I tried to debug the code by adding an alert, things went strange and the alert appeared before the confirmation:

function link() {
    var tmp = cnf("Cancel changes?", emptyfn);
    alert(tmp);
    return tmp;
}

How can I correct the code, and why does my debugging code get executed out of order (is it to do with the return value in the function) ?

gornvix
  • 3,154
  • 6
  • 35
  • 74
  • 1
    Impossible..... There is no wait in JavaScript so you can not pause an asynchronous call, that is why the script has callbacks for when the user makes a choice. – epascarello Aug 11 '16 at 17:54
  • @epascarello As far as I know if call to a native alert it will stop script execution, untill you close alert window – Nikita Aug 11 '16 at 17:58
  • 1
    Yes, because that is how the native alert, confirm, prompt work. You can not recreate that functionality in JavaScript. – epascarello Aug 11 '16 at 17:59
  • Oh that is a bummer, I hate the native alert and confirm, they look rubbish and the buttons can't be customised. – gornvix Aug 11 '16 at 18:02
  • then don't use it, and properly code for the better looking one. – Kevin B Aug 11 '16 at 18:12
  • @KevinB how can it be a duplicate of mentioned when OP didnt use any `async` calls – Ja9ad335h Aug 11 '16 at 18:20
  • @JAG what do you think `$.confirm` is? ajax isn't the only thing that can be asynchronous. – Kevin B Aug 11 '16 at 18:20

1 Answers1

-2

Your code doesn't work because javascript will not wait till $.confirm executed because it is an async function. The simplest way to use callback function.

function workWithRes() {
...
res = true;
//and some stuff that should executed after confirm created
}
var res = false;

function cnf(cntn, fn, prm, callbackFn) {
    $.confirm({
        title: "",
        content: cntn,
        confirmButton: "Yes",
        cancelButton: "No",
        confirm: function() { fn(prm); callbackFn();  },
        cancel: function() { }
    });
}

For some other solutions read about async calls in javascript.

Nikita
  • 1,019
  • 2
  • 15
  • 39
  • OK, I don't agree with the use of the res variable that was a mistake on my part. But yes using a callback containing document.getElementById("form").submit() does the trick. – gornvix Aug 11 '16 at 18:21