1

I need to pause a JavaScript function execution in the middle and then resume it after a button click. Please help me.

3 Answers 3

1

This isn't possible.

Break the function up in to two parts, run one, and have the other assigned to the click event handler of the button.

Sign up to request clarification or add additional context in comments.

5 Comments

actually my requirement is to implement light box for window.confirm, as the window.confirm can pause the function and return true or false I would like to implement the same
@kumar, It's pretty much the same procedure as @David Dorward described, except instead of assigning a click handler, you're adding a callback to the dialog.
but I have to return true or false from the current javascript function on click of the light box yes or no buttons..
@kumar, You can pass true or false as an argument to the callback.
@kumar: You can't write a linear program in an event driven programming language when you need to deal with a user event in the middle of it. You need to rethink your program structure.
1

You could use a pop up box.

alert("Pausing to get Coffee");

1 Comment

thanks for your reply but I need to implement customized window.cofirm I am showing a light box in the mean time the function is not pausing its execution, I would like to set a hidden variable in html to true or false on click of yes or no on light boxes, and on resume of the function I will read the variable and do necessary action.
1

Like David said, it is not possible to stop execution of a function in Javascript (well, not at the moment anyway). One solution would be this :

** EDITED ** after you added some precision to what you wanted to do

// pass the reference of the button id to the function
function showConfirm(message, callback) {
   // 1. check if the lightbox is not already created, if not create it
   // 2. keep a reference to your key elements, for example, your buttons
   var btnOk = document.getElementById('btnOk');  // ...for example
   var btnCancel = document.getElementById('btnCancel');  // ...for example

   // 3. have a 'cleanup' function so you can dismiss your lightbox, unregister
   //      any events/callbacks, etc.
   var cleanup = function() {
      // 6. hide lightbox
      // 7. remove events/callbacks, etc.
      btnOk.click = null;  // for example
      btnCancel.click = null; // for example

      // etc.
   };


   // 4. update your lightbox with the message given in argument

   // 5. register some events to your buttons...
   btnOk.click = function() {
      callback(true);  // ok was pressed
      cleanup();
   };
   btnCancel.click = function() {
      callback(false); // cancel was pressed
      cleanup();
   }

}

All you have to remember is that, in Javascript, everything should be asynchronous. If your function should return a value, it should be a function that does not require long to execute. As soon as you read "user input" with Javascript, you need callbacks. You might want to take a look at how other lightbox implementations are done, especially in frameworks like JQuery, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.