简体   繁体   English

JavaScript 是否有 sleep() 解决方法?

[英]is there a sleep() workaround for JavaScript?

So that I could use a loop, and have it pause, say 250 ms after each iteration?这样我就可以使用一个循环,让它暂停,比如每次迭代后 250 毫秒? [ without setTimeout or setInterval?] [没有 setTimeout 或 setInterval?]

Edit: Ok, so I have a for loop that uses AJAX to post data ( $.ajax() ).编辑:好的,所以我有一个 for 循环,它使用 AJAX 来发布数据( $.ajax() )。 It has lots of vars in it, like data: 'uid=' + uid , + '&othervars=' + morevars etc, and each loop, the variables change .. so If put that [huge] part into a function() {} [Inside of a setTimeout(), using a counter in the loop to increment the timeout], when it executes, will the actual value be in place where I put the vars?它里面有很多变量,比如data: 'uid=' + uid , + '&othervars=' + morevars等等,每个循环,变量都会改变..所以如果把那个 [huge] 部分放入 function() { [在 setTimeout() 内部,使用循环中的计数器来增加超时时间],当它执行时,实际值会在我放置变量的地方吗?

Current browser implementations are essentially single-threaded, so sleeping in your code will likely block the entire UI.当前的浏览器实现本质上是单线程的,因此在代码中休眠可能会阻塞整个 UI。 It's not what you want, and just plain isn't how JS works.这不是你想要的,而且 JS 的工作方式也不是那么简单。 Refactor your code so that you can use setTimeout.重构您的代码,以便您可以使用 setTimeout。

You will need to use one of javascript's timer functions ( setTimeout or setInterval ).您将需要使用 javascript 的计时器函数之一( setTimeoutsetInterval )。

There is no real way around them.没有真正的方法可以绕过它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM