this is urgent and i couldn't find a solution to it. The problem is as follow:
i am designing a web app, which sends multiple mands to multiple machines. Now some of the mands i have require additional input. i have designed a pop up window in jquery that ask user to add this additional input. the problem is while looping through all the mands only a window for the last chosen mand pops up... this is because it is not pausing while user enter inputs before going to input to another mand.
how can i pause a function from continuing its execution in javascript/jquery?
an example in pseudocode:
loop each mand
{
for selected mand popup windows;
// pause until user finishes input then go to
// next line where any function is processed
function(); //then loop again--> pause --> when user finishes continue to function() etc..
}
thx for your help and patience, i tried all kinds of methods without any result. :)
this is urgent and i couldn't find a solution to it. The problem is as follow:
i am designing a web app, which sends multiple mands to multiple machines. Now some of the mands i have require additional input. i have designed a pop up window in jquery that ask user to add this additional input. the problem is while looping through all the mands only a window for the last chosen mand pops up... this is because it is not pausing while user enter inputs before going to input to another mand.
how can i pause a function from continuing its execution in javascript/jquery?
an example in pseudocode:
loop each mand
{
for selected mand popup windows;
// pause until user finishes input then go to
// next line where any function is processed
function(); //then loop again--> pause --> when user finishes continue to function() etc..
}
thx for your help and patience, i tried all kinds of methods without any result. :)
Share Improve this question edited Jun 23, 2010 at 21:54 nickf 547k199 gold badges658 silver badges727 bronze badges asked Jun 23, 2010 at 21:31 ccotccot 1,8733 gold badges37 silver badges54 bronze badges 1- Post your existing code. – Matthew Flaschen Commented Jun 23, 2010 at 21:42
4 Answers
Reset to default 3Can't you use a prompt
dialog to get user input? It is modal and will pause execution at the point where it is placed unless the user cancels the prompt, or provides a value. And please don't call it ugly.
var userValue = prompt("Ask your question");
See example here. Wait a while before you enter a value or cancel, and notice the timestamps.
This is typically done using callback functions. Since you're using jQuery already, you might find something like the Impromptu plugin helpful. It lets you do modal prompts with callbacks.
Something like (partially based on example 9 from above link). I posted this as a demo at http://jsfiddle/28d3C/2/ .
var ind = 0;
var values = [];
var count = 3;
function nextPrompt(done)
{
function processPrompt(v, m, f)
{
if(v != undefined)
{
var input = f.alertName;
values[ind] = input;
}
if(++ind < count)
{
nextPrompt(done);
}
else
{
done();
}
}
var txt = 'Please enter value ' + ind + ': <br /><input type="text" id="alertName" name="alertName" value="name here" />';
$.prompt(txt,{
callback: processPrompt,
buttons: { Hey: 'Hello', Bye: 'Good Bye' }
});
}
nextPrompt(function()
{
for(var i = 0; i < count; i++)
{
alert(values[i]);
}
});
The short answer is that you can't 'pause' Javascript execution. You can do something like this if it emulates a pause well enough for your purposes, but it is not the same thing.
var answer;
var myInterval = setInterval(function () {
if (answer != undefined) {
clearInterval(myInterval);
// processing code
}
}, 1000);
answer = prompt("Please enter your name.");
If I understand you correctly, I think you should launch the window using window.showModalDialog() instead of window.open().
It will hold the execution until the dialog is closed.
An alternative way is to use the callback function .