I need to pause a JavaScript function execution in the middle and then resume it after a button click. Please help me.
+1
A:
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.
David Dorward
2010-08-21 12:25:21
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
2010-08-21 12:35:28
@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.
strager
2010-08-21 12:42:32
but I have to return true or false from the current javascript function on click of the light box yes or no buttons..
kumar
2010-08-21 12:49:11
@kumar, You can pass `true` or `false` as an argument to the callback.
strager
2010-08-21 12:52:38
@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.
David Dorward
2010-08-21 13:06:10
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.
kumar
2010-08-21 12:41:59
+1
A:
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.
Yanick Rochon
2010-08-21 12:49:38