0

I am looking the way to stop the progress of button click until confirmation had made.

Please advice a way to stop the progress temporary until 'Show Confirm Box' return true. Now my function will keep running forward regardless it.

.click(function(){
    //Show Confirm Box <- will return true of false

    //Start Process if true;

});

Thank you very much.

Micah
  • 4,254
  • 8
  • 30
  • 38
  • do you mean you want to stop the click event from occurring, e.g. intercepting a click on a form submission button, or simply you want to wait for some other event in your code (i.e. your own dialog box)? – kieran Dec 27 '12 at 06:34
  • I believer, I want to wait another function to give it a value, true of false from 'Show Confirm Box()' – Micah Dec 27 '12 at 06:46

3 Answers3

2

Calls to confirm() are synchronous. That is, until the user closes the box, your code is effectively paused.

if (confirm('some message')) {
    // User confirmed, do something here
}

Now, if you are not using confirm(), and are instead using your own custom modal dialog or something similar, then you will need to use callbacks.

You should not block your script from executing, as the page will appear locked up to the user. Instead, pass a callback function to your function that shows your dialog, and let your dialog call that function when you are done.

function showDialog (confirmCallback) {
    // Show dialog here
    if (result === 'yes') { // replace this, obviously
        confirmCallback();
    }
}

.click(function(){
    showDialog(function () {
        // start process
    });
});
Brad
  • 159,648
  • 54
  • 349
  • 530
  • Thank you very much, I an a beginner of js, may I ask an additional question? confirmCallbac here is not a parameter but its like a system parameter, and its same to confirmCallback(), And confirmCallback() will automatically go back to .click showDialog(). Am I understand this right? – Micah Dec 27 '12 at 06:57
  • No, `confirmCallback` is in fact a parameter. In JavaScript, you can assign functions to variables, just like you can any other object. In the click handler, when `showDialog()` is called, you are actually passing an anonymous function as the first parameter to `showDialog()`. That function gets assigned to `confirmCallback` within `showDialog()`, just like any other value to any other parameter would. – Brad Dec 27 '12 at 07:00
1

The parameter to the click event is a function handler which will get executed when the click event occurs.

So You can always return from that function when the confirmation dialog is returned a false value.

Code will be like this

jQuery(".button").click( function(){
   var ans = confirm("Do you want to proceed further ?");
   if(!ans) return;

   alert("Now you can code the rest ");

});​

I've created a fiddle , check this below

http://jsfiddle.net/shidhincr/Ubj7S/1/

Shidhin Cr
  • 868
  • 8
  • 19
  • Thank you very much, due to I want to use my own dialog, and I create a function like var ans = Show-dialog() instead of confirm(). The thing is I cnt not how to stop the entire progress before if(!ans) return; – Micah Dec 27 '12 at 07:02
0

did you see this question? pausing execution in custom confirm box

just split up the code and call the functions according to the users input from the confirm box

Community
  • 1
  • 1
jkofron.e
  • 5,043
  • 1
  • 17
  • 16