Javascript页面在打开新窗口后停止响应

Javascript页面在打开新窗口后停止响应,javascript,Javascript,在我的Javascript代码中,我有一个无限while循环,它单击一个按钮,从几个文本框收集值,并将它们发送到PHP脚本。我不知道怎么做,但是为PHP脚本打开一个新窗口。一切都在本地主机上运行。因此,我的代码如下所示: <body onload="generator();"> ... function generator(){ phpWindow = window.open("http://localhost/adressSaver.php", "", "width=20

在我的Javascript代码中,我有一个无限while循环,它单击一个按钮,从几个文本框收集值,并将它们发送到PHP脚本。我不知道怎么做,但是为PHP脚本打开一个新窗口。一切都在本地主机上运行。因此,我的代码如下所示:

 <body onload="generator();">
...
  function generator(){
  phpWindow = window.open("http://localhost/adressSaver.php", "", "width=200, height=100");
  while(true){
      document.getElementById("genRandom").click();
      var first = document.getElementById("first").value;
      var second = document.getElementById("second").value;
      phpWindow.location = "http://localhost/adressSaver.php?first=" + first + "&second=" + second;
      }
}

...
函数生成器(){
phpWindow=窗口打开(“http://localhost/adressSaver.php“宽度=200,高度=100”);
while(true){
document.getElementById(“genRandom”)。单击();
var first=document.getElementById(“first”).value;
var second=document.getElementById(“second”).value;
phpWindow.location=”http://localhost/adressSaver.php?first=“+first+”&second=“+second;
}
}
我假设使用这个函数,每个周期变量都会传递给PHP脚本。相反,当我打开这个文档时,为PHP创建了一个新窗口,然后这两个窗口一直在加载,什么都不做。我甚至尝试取消循环,使其成为一次性操作,没有任何更改。 有什么想法吗


谢谢

在这种情况下,您必须使用
setInterval
,这样您的代码就不会无限期地阻止执行:

function generator() {
  phpWindow = window.open("http://localhost/adressSaver.php", "", "width=200, height=100");
  window.setInterval(function () {
      while(true) {
          document.getElementById("genRandom").click();
          var first = document.getElementById("first").value;
          var second = document.getElementById("second").value;
          phpWindow.location = "http://localhost/adressSaver.php?first=" + first +     "&second=" + second;
      }
}}, 10);

这将导致您的代码每10毫秒执行一次,您可以随意更改代码。将其更改为0将导致代码毫不延迟地执行(尽管实际上存在延迟,因为javascript不是多线程的)。

这是因为javascript和页面呈现只有一个线程。这意味着,当循环处于活动状态时,不会发生任何事情:任何其他JS(如鼠标单击等)或任何DOM节点渲染。
要修复它,只需为循环添加一个“中断”:

function generator(){
    phpWindow = window.open("http://localhost/adressSaver.php", "", "width=200, height=100");
    setInterval(function() {
        document.getElementById("genRandom").click();
        var first = document.getElementById("first").value;
        var second = document.getElementById("second").value;
        phpWindow.location = "http://localhost/adressSaver.php?first=" + first + "&second=" + second;
    }, 1000);
}

这将每秒执行代码。

感谢您的回复。现在主窗口加载正常,但是phpWindow加载并保持空闲,传递变量的重定向不起作用,你能帮我吗?我的错误,10毫秒太短了,尝试了100秒,现在工作正常。谢谢