0

我不能把头绕在这个上面吗?
为什么这是我的:
.each 循环会一直运行,即使我在每个循环中将事情拖延 1000 毫秒?
问题是 window.location.href 命令在 setTimeout完成之前运行到 EARLY吗?也提前结束的 stopload() 函数也是如此?我已经看到一些关于递归 setTimeout 函数的内容是这里需要什么以及如何实现它?

function shop(clickedButton)
{
  var buttonvalue = $(clickedButton).val();
  startLoad();
  pdel = 1000;

  $("input:submit[value='buy']").each(function(index)
  {
    if(index != 1)
    {

       $("#backgroundPopup").text(index);
       var submithing = this;  
       setTimeout(function(){ clicksubmitbutton(submithing); },pdel);
       pdel += 1000;
    }                      
  });   

  stopLoad();

  if(buttonvalue == "1")
  {
     window.scrollTo(0,0);
  }
  else
  {
     window.location.href = 'http://my.url';
  }                    

}

4

1 回答 1

2

Javascript 没有休眠或停顿的概念。在调用之后继续执行setTimeout。这个函数只是安排一个函数在给定的毫秒数之后运行。

为了获得您想要的行为,您需要在setTimeout回调函数中调用下一次迭代。

就像是:

function submitTheThing(index) {
    if (done) { //some logic
        return;
    }

    // do something

    setTimeout(function() { submitTheThing(index+1); }, 1000);
}
于 2013-04-01T23:53:22.210 回答