本文介绍了如何在jQuery.each函数的每个循环之间进行延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

$('li').each(function(){
   var data = $(this).text();
   requestFunction(data, function(status){
       if ( status == 'OK' ) do stuff...
   });
});

因此,我需要在使用函数requestFunction()之间做一些延迟。我怎么能这样做?希望这是可以理解的,谢谢。

So, i need to do some delay between using function "requestFunction()". How could i do this? Hope it understandable, thanks.

推荐答案

增加时间的setTimeout:

setTimeout at an increase time:

$('li').each(function(indexInArray){
   var data = $(this).text();
   setTimeout( function () {
       requestFunction(data, function(status){
           if ( status == 'OK' ) do stuff...
       });
   }, indexInArray * 500);
});






如果你循环遍历这些元素,我们想要增加超时,否则所有请求都会在没有延迟的情况下同时触发,但仅在我们的500毫秒超时后才会触发。


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.


  • 时间开始: 0 ms

  • 第一次请求: 0 ms(500 * 0)

  • 秒要求: 500毫秒(500 * 1)

  • 第三次请求: 1000毫秒(500 * 2)

  • Time Start: 0 ms
  • First Request: 0 ms (500 * 0)
  • Second Request: 500 ms (500 * 1)
  • Third Request: 1000 ms (500 * 2)

这篇关于如何在jQuery.each函数的每个循环之间进行延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 15:58