Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在函数结束之前阻止JS代码运行_Javascript_Jquery - Fatal编程技术网

Javascript 在函数结束之前阻止JS代码运行

Javascript 在函数结束之前阻止JS代码运行,javascript,jquery,Javascript,Jquery,我不能把我的头绕在这个上面 为什么我的: .每个循环都会直接运行,即使我在每个循环中使内部延迟1000毫秒? 问题是window.location.href命令在setTimeout完成之前运行到早?stopload()函数也是如此,它也结束为early?我看到了一些关于递归setTimeout函数的东西,这里需要什么,我如何实现它 function shop(clickedButton) { var buttonvalue = $(clickedButton).val(); star

我不能把我的头绕在这个上面
为什么我的:
.每个循环都会直接运行,即使我在每个循环中使内部延迟1000毫秒?
问题是window.location.href命令在setTimeout完成之前运行到早?stopload()函数也是如此,它也结束为early?我看到了一些关于递归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';
  }                    

}Javascript没有睡眠或暂停的概念。执行将在调用
setTimeout
后继续。此函数只是计划一个函数在给定的毫秒数之后运行

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

比如:

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

    // do something

    setTimeout(function() { submitTheThing(index+1); }, 1000);
}

setTimeout
不会暂停任何操作。您只需告诉JavaScript在当前脚本执行终止后,将来是否执行某些操作。如果要在超时后执行某些操作,则必须在超时后将其放入/call。