1

I am trying to make a code for my website

function vind(list) {
    for (i = 0; i < list.data.length; i++) {
        jQuery.ajax({
            url: 'https://xyz-abc.com/send.php?post=123',
            dataType: 'script',
            success: function () {
                volve += 1;
                if (volve >= list.data.length) {
                }
            }
        });
    }
}

all code is working fine.

https://xyz-abc.com/send.php?post=123
https://xyz-abc.com/send.php?post=123
https://xyz-abc.com/send.php?post=123
https://xyz-abc.com/send.php?post=123

code posting continous.

I want to set Interval between each post.

https://xyz-abc.com/send.php?post=123
wait 3 sec.
https://xyz-abc.com/send.php?post=123
wait 3 sec.
https://xyz-abc.com/send.php?post=123
wait 3 sec.

I tried setTimeout not working.

Help.

Howard Renollet
  • 4,609
  • 1
  • 24
  • 31
Vinay K
  • 13
  • 3

1 Answers1

1

If you first want to wait for the previous request to complete you can send the 1st item and use setTimeout to trigger a request on the rest of the list.

function vind(list) {
  if (list.length > 0) { // stop if the list is empty
    var firstItem = list.shift(); // nothing is done with this?
    jquery.ajax('https://xyz-abc.com/send.php?post=123', {
      dataType: 'script',
      success: function() {
        setTimeout(function() { // set a timeout with a function that will call sendData again
          sendData(list); // call sendData with the rest of the list
        }, 3000); // 3 seconds
      }
    });
  }
}

You can check a working plnkr of that here.

If you do not want to wait for the previous requests to complete you can just use setTimeout to set a bunch of timeouts with different delays. I would not recommend doing this for large lists though.

function sendData(item) {
  console.log(Date.now());
  jquery.ajax('https://xyz-abc.com/send.php?post=123', {
    dataType: 'script',
    success: function() {}
  });
}

function vind(list) {
  for (var i = 0; i < list.length; i++) {
    var currentItem = list[i]; // saving the item in the current scope now, in case the list gets modified in the meantime
    setTimeout(function() {
      sendData(currentItem);
    }, i * 3000); // delay depends on the item's index in the list
  }
}
And a working plnkr of this solution here.

Keep in mind though setTimeout does not guarantee that the set function will be called exactly at the 3 seconds mark. What it does is to place that function in the callback queue after 3 seconds have passed. That means because of the way javascript concurrency works won't be able to get a function called exactly at the 3 seconds mark using setTimeout.

You can find a very good explanation on how the event loop works here. Also there's a tool made by the same guy that gives a visual representation of what is going on behind the scene here.

toskv
  • 30,680
  • 7
  • 72
  • 74
  • That's not syntactically or logically valid code. – Felix Kling Sep 25 '15 at 19:45
  • oups, you're right. I'll fix it. :) – toskv Sep 25 '15 at 19:46
  • @FelixKling better now? :) – toskv Sep 25 '15 at 19:54
  • @toskv no sir still not getting in intervals. – Vinay K Sep 26 '15 at 04:06
  • @Vinay K The solution 1st waits for the previous request to complete before setting a timeout of 3 seconds. That means the time it takes to complete the request will add to those 3 seconds. Also **setTimeout** does not guarantee that the function will be called exactly at the 3 second mark, it only guarantees it won't be called earlier than 3 seconds. You can check this plunker for a working example, it takes about 3.2 seconds to between requests. http://plnkr.co/edit/LPcJMEwIk2mJUEoLaYsB?p=preview – toskv Sep 26 '15 at 13:59
  • @VinayK I added a solution that does not wait for previous requests. – toskv Sep 26 '15 at 14:22