9

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.

Ax.
  • 320
  • 3
  • 4
  • 12

2 Answers2

26

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);
});

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.

  • Time Start: 0 ms
  • First Request: 0 ms (500 * 0)
  • Second Request: 500 ms (500 * 1)
  • Third Request: 1000 ms (500 * 2)
Joe
  • 80,724
  • 18
  • 127
  • 145
  • What Array it would? I mean "indexInArray". – Ax. Sep 16 '11 at 14:16
  • Oh, man, you just forget to write "each(function( *indexInArray* ){" this part of code. I know about index'es in jQ.each function. So, now i understad you well. Thanks. Will try it out. – Ax. Sep 16 '11 at 14:26
  • @Ax, yeah, I had mistyped that. Let me know how it goes – Joe Sep 16 '11 at 14:28
  • Thanks man! It works well :D now Google Geocoder don't result with a status == "OVER_QUERY_LIMIT" :D Haha, thanks a lot :) But i make delay a little more for a good result id * 800 – Ax. Sep 16 '11 at 14:35
2

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 to false.

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.

Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
  • I can't attempt to read a source of "requestFunction". I just can make some delay between it calls. – Ax. Sep 16 '11 at 14:20
  • Why do you need a delay between calls? Please explain – Jamie Dixon Sep 16 '11 at 14:20
  • Yes. :) But i just wanna do it some pretty way. It would be ideal if it would be able to do delay between loops in jQ.each function. – Ax. Sep 16 '11 at 14:23
  • Why do you want to do this thought? Please explain why. If you explain why, we may be able to help you more easily. – Jamie Dixon Sep 16 '11 at 14:25
  • "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." Man, this is pretty stupid :) I know what is callback function :) – Ax. Sep 16 '11 at 14:28
  • "Not understand, sory." - That is your first comment. I cannot read your mind Ax. – Jamie Dixon Sep 16 '11 at 14:30
  • My first comment was for your first version of answer, without any edits. But, nevermind, i'm ok already. Thanks for help. – Ax. Sep 16 '11 at 14:34
  • No problem. That's why I added the edit because I didn't know which bit you didn't understand. Glad you've got it sorted. – Jamie Dixon Sep 16 '11 at 14:34
  • Thanks for a focus again. The function was Google.Map Geocode – Ax. Sep 16 '11 at 14:36
  • was running into typical firefox aborting a slew of consecutive ajax requests and `sync: true` works. – But those new buttons though.. Feb 11 '15 at 23:09