如何在 jQuery.each 函数的每个循环之间设置延迟?
我有这样的代码:
$('li').each(function(){
var data = $(this).text();
requestFunction(data, function(status){
if ( status == 'OK' ) do stuff...
});
});
所以,我需要在使用函数“requestFunction()”之间做一些延迟。我怎么能这样做呢?希望能理解,谢谢。
I have this-like code:
$('li').each(function(){
var data = $(this).text();
requestFunction(data, function(status){
if ( status == 'OK' ) do stuff...
});
});
So, i need to do some delay between using function "requestFunction()". How could i do this? Hope it understandable, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
setTimeout 增加时间:
如果循环这些元素,我们希望增加超时,否则所有请求如果没有延迟,都会同时触发,但只有在 500 毫秒超时之后才会触发。
setTimeout at an increase time:
if you loop over these elements, we want to increase the timeout or else all the request would fire at the same time if not delayed, but only after our 500 ms timeout.
如果您在
each
循环中进行 ajax 调用,那么您可能需要同步运行 ajax 请求。为此,您可以将 ajax 请求的
async
属性设置为false
。或者,您可能想考虑实现
requestFunction
的回调。这将允许您在方法返回后运行代码,并且不需要任何超时等。回调基本上是在代码末尾执行的方法。你基本上告诉你的函数,这是我希望你在完成工作后调用的另一个函数。
If you're making ajax calls within your
each
loop then you may want to run the ajax requests syncronously.To do this you can set the
async
property of the ajax request tofalse
.Alternatively you may want to look into implimenting a callback for
requestFunction
. This will allow you to run code after your method has returned and will negate the need for any timeout etc.A callback is basically a method that gets executed at the end of your code. You basically tell your function, here's another function I want you to call when you've finished doing your work.