0

这很紧急,我找不到解决办法。问题如下:

我正在设计一个网络应用程序,它将多个命令发送到多台机器。现在我的一些命令需要额外的输入。我在 jquery 中设计了一个弹出窗口,要求用户添加这个额外的输入。问题是在循环所有命令时,只会弹出最后一个选择的命令的窗口......这是因为在用户输入输入之前它不会暂停,然后再输入另一个命令。

如何暂停函数继续在 javascript/jquery 中执行?

伪代码示例:

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..

}

感谢您的帮助和耐心,我尝试了各种方法,但没有任何结果。:)

4

4 回答 4

3

你不能使用prompt对话框来获取用户输入吗?它是模态的,除非用户取消提示或提供值,否则它将在放置它的位置暂停执行。请不要说它丑陋。

var userValue = prompt("Ask your question");

请参见此处的示例。在输入值或取消之前稍等片刻,并注意时间戳。

于 2010-06-23T21:48:00.673 回答
2

这通常使用回调函数来完成。由于您已经在使用 jQuery,您可能会发现像Impromptu插件这样的东西很有帮助。它允许您使用回调进行模态提示。

类似的东西(部分基于上面链接中的示例 9)。我将此作为演示发布在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]);   
    }
});
于 2010-06-23T21:51:22.977 回答
1

简短的回答是您不能“暂停”Javascript 执行。如果它可以很好地模拟暂停以满足您的目的,您可以执行类似的操作,但它不是一回事。

var answer;

var myInterval = setInterval(function () {
  if (answer != undefined) {
    clearInterval(myInterval);
    // processing code
  }
}, 1000);

answer = prompt("Please enter your name.");
于 2010-06-23T21:35:45.543 回答
0

如果我理解正确,我认为您应该使用 window.showModalDialog() 而不是 window.open() 来启动窗口。
它将保持执行直到对话框关闭。

另一种方法是使用回调函数。

于 2010-06-24T02:49:01.260 回答