How to pause a function in javascript until a CONDITION is met, not until a defined limit in millisecond is set!

StackOverflow https://stackoverflow.com/questions/3105709

Question

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 commands to multiple machines. Now some of the commands 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 commands only a window for the last chosen command pops up... this is because it is not pausing while user enter inputs before going to input to another command.

how can i pause a function from continuing its execution in javascript/jquery?

an example in pseudocode:

loop each command
{
  for selected command 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. :)

Was it helpful?

Solution

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 .

OTHER TIPS

Can'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.net/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.");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top