如何在每次提交后保持10秒的延迟

时间:2012-11-07 03:52:30

标签: javascript jquery

我在下面有这个Javascript

想要在FireFox中工作 我想在每次提交后保持5秒延迟

<html>
 <head>
 <script type="text/javascript">
function sleep(ms)
    {
        var dt = new Date();
        dt.setTime(dt.getTime() + ms);
        while (new Date().getTime() < dt.getTime());
    }

function test() {
var windowCounter = 1; 
var myStringArray = [ "user1", "user2" , "user3" , "user4" ]
var len = myStringArray.length;
for (var i=0; i<3; ++i) {
document.inform.cid = myStringArray[i];
document.inform.pwd = "xxxxxxxx";
   document.inform.target = windowCounter++; // a different target each time
    document.inform.submit();
}
}
</script>
 </head>
 <body >
  <form name="inform"   target="newWin" action="https://www.google.co.in/">
<input type="text" name="cid"  />
<input type="hidden" name="pwd"  />
 <input type="hidden" name="throttle" value="999" />
    <input type="submit" value="go" onclick="test()">
  </form>
 </body>
</html>

我尝试在每次提交后手动保持睡眠并尝试使用setTimeOut,但没有任何效果。

任何人都可以帮助我

已编辑的部分

<html>
<head>
<script type="text/javascript">
  var interval = window.setInterval(iterate, 5000);
var myStringArray = ["user1", "user2", "user3", "user4"];

  function iterate() {
 iterate.arr = iterate.arr || myStringArray.slice(0);

    //if it still has elements left
    if(iterate.arr.length > 0) {
        document.inform.cid = iterate.arr.pop(); //remove the top one
alert(document.inform.cid);
        document.inform.pw = "xxxx";
        document.inform.target = iterate.arr.length; // a different target each time - length of the arr
        document.inform.submit();
    } else {
        window.clearInterval(interval); //no more left cancel it
    }
};

</script>
</head>
<body>
 <form name="inform" method="get"  target="newWin" action="https://www.google.co.in/">
  <input type="text" name="cid" />
<input type="password" name="pw" />
<input type="hidden" name="throttle" value="999" />
    <input type="submit" value="go" onclick="iterate()"/>
  </form>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

你可以使用类似的东西(不测试):

    var interval = window.setInterval(iterate, 5000);
    var myStringArray = ["user1", "user2", "user3", "user4"];

function iterate() {
    iterate.arr = iterate.arr || myStringArray.slice(0); //set a private array to cache

    //if it still has elements left
    if(iterate.arr.length > 0) {
    //thought there was more than one formon the page - but if only one then we can reference by its name - cid

        ///:document.inform.cid = iterate.arr.pop(); //remove the top one
    iterate.arr.pop();
        document.inform.pwd = "xxxxxxxx";
        document.inform.target = iterate.arr.length; // a different target each time - length of the arr
        document.inform.submit();
    } else {
        window.clearInterval(interval); //no more left cancel it
    }


};

答案 1 :(得分:0)

请尝试这个:

<html>
     <head>
     <script type="text/javascript">
    function submit()
        {
             document.inform.submit();
        }

    function test() {
     setTimeout('submit()',5000);
     }
    </script>
     </head>
     <body >
      <form name="inform"  target="newWin"  action="https://www.google.co.in/">
    <input type="text" name="cid"  />
    <input type="hidden" name="pwd"  />
     <input type="hidden" name="throttle" value="999" />
        <input type="button" value="go" onclick="test()">
      </form>
     </body>
    </html>