views:

107

answers:

4

hello everybody, 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. :)

+1  A: 

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.");
g.d.d.c
@g.d.d.c - You don't really need to set any intervals here. the last line `var answer = prompt("Please enter your name.");` by itself is sufficient.
Anurag
@anurag - I added the setInterval code to demonstrate how to wait for a condition. Prompt will pause processing, but it wasn't entirely clear whether the OP would be using prompt. If they did something like opened a jQuery dialog with an input field, and the close event handler of the dialog assigned the input field value to his variable then this code would be highly applicable. :)
g.d.d.c
+2  A: 

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.

Anurag
"Please don't call it ugly." Why not? It is. It's useful for debugging, but it's almost never appropriate for production code.
Matthew Flaschen
@Matthew - Most browsers have pretty advanced debugging capabilities, so prompt is literally useless in that arena. Why isn't it appropriate for production code? It keeps your code simple. Is ideal for string input. Has autofocus on the input control, and regular shortcuts (Esc/Enter) work as expected. Not all production level applications are built with prettiness in mind. And all the above points indicate that it's pretty damn usable.
Anurag
It's clearly useful for providing input while debugging and during early development. I just don't think it fits for most production apps. There are solutions with the features you listed, and more. prompt has limitations besides "prettiness.", like only allowing one field at a time.
Matthew Flaschen
@Matthew - I agree that it is not an appropriate solution for all cases, but for this particular case where the user must enter a value for something to happen and it has to be modal, it seems like a pretty logical choice to me. You can see an example similar to the one you posted here - http://jsfiddle.net/3xEGp/
Anurag
+2  A: 

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]);   
    }
});
Matthew Flaschen
A: 

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 .

Kai
thx... i used it, the problem is once the dialog page is closed it closes all javascript so , in my case i am getting values from the modal dialog which i will pass as parameters to a function in my js file... but wt is happening when i close is that at some point it doesnt run the js function anymore. Is there a way to reference this modaldialog so that i can close it without it affecting the rest of the program?
Chadic