3

I'm trying to get each array link on some interval. For example: I get first link, wait for 30 seconds, next link, wait for 30 seconds another link etc. Here is my code:

var urls = [ 'http://mylink1','http://mylink2','http://mylink3','http://mylink4'];

setInterval(function(){
        urls.forEach(function(entry){
            console.log(entry);
            ajaxd(entry);
            console.log("merge pana aici");
        });
    },30000);

function ajaxd(my_url) {
    $.ajax({
        type : "POST",
        url : my_url,
        success : function(msg) {
            console.log(my_url);
        }
    });
}

And the problem is, after 30 seconds I get all the links. Not first value, wait for 30 seconds, next value and so on..

Husen
  • 929
  • 1
  • 6
  • 16

7 Answers7

3

EDIT Code updated to cause the loop to repeat.

(function() {

  var urls = ['http://mylink1', 'http://mylink2', 
              'http://mylink3', 'http://mylink4'];

  // Start off at the first element.
  var idx = 0;
  var len = urls.length;

  // Do the next link
  function doNext() {
    var entry = urls[idx];

    console.log(idx + ":" + entry);
    //ajaxd(entry);

    idx++;
    console.log([idx, len]);
    if (idx < len) {
      // Don't do anything special
    }  else {
      // Reset the counter
      idx = 0;
    }
    setTimeout(doNext, 300);      }

  // And the code needs kicked off somewhere
  doNext();

}());//end of function 
Jeremy J Starcher
  • 21,760
  • 5
  • 48
  • 70
  • thank you @Jeremy J Starcher . this is a very good answer, but I've needed a loop, after link4 repeat the array. :) –  Aug 12 '14 at 07:01
  • @Andrei Takes a minimal change to cause it to repeat... I updated the code to handle that. – Jeremy J Starcher Aug 12 '14 at 07:04
  • @JeremyJStarcher Can you help me, if I want a pause after link4.. for example: link4 -- wait for 60seconds -- (repeat the array?:)) –  Aug 12 '14 at 07:44
  • You have all the tools you need to add that feature. Give it a try and if you can't figure it out then it should be asked as a new question. If you can't sort it out on your own, http://stackoverflow.com/questions/16873889/how-to-create-javascript-delay-function – Jeremy J Starcher Aug 12 '14 at 07:46
  • I'm using this code in the console to invite my friends to like my page on Facebook, at a 90s interval, to stay off their radar. Beats hand clicking on 500 buttons ;) Thanks Jeremy. – jamesJosephFinn Jul 05 '16 at 20:55
1

Making a foreach will iterate through all the values in the array instantly after the interval.

A better solution is not to iterate...

e.g

var counter = 0;

setInterval(function(){
  if(counter < url.length){
    ajaxd(url[counter]);
    counter++;
  }else
    return;
}, 30000);
Xlander
  • 1,243
  • 12
  • 24
  • Best answer so far to make it an infinite loop just set counter to 0 in else condition and it will restart again exactly what i needed Thanks mate – Awais fiaz May 10 '18 at 21:34
1

If you still want to use forEach to loop the array you can use it with setTimeout not with setInterval.

var urls = ['http://mylink1', 'http://mylink2', 'http://mylink3', 'http://mylink4'],
    interval = 2000, //  = 2s
    increment = 1;

urls.forEach(function(url) {
  var runner = setTimeout(function() {
    // Do your stuff.
    console.log(url);

    clearTimeout(runner);
  }, interval * increment);

  increment = increment + 1;
});
Tpn Knvl
  • 43
  • 4
0

In your closure you use a for loop to request all the items at once.

Charles Smartt
  • 349
  • 2
  • 8
0

You are doing it wrong try creating a counter every 30 sec. check this code:

$(document).ready(function(){

    var urls = [ 'http://mylink1','http://mylink2','http://mylink3','http://mylink4'],
        counter = 0;

        obj = {}

    obj.ajaxd = function(my_url) {
        $.ajax({
            type : "POST",
            url : my_url,
            success : function(msg) {
                console.log(my_url);
            }
        });
    }

    obj.leftInterval = setInterval(function(){
        if(urls[counter] != undefined){
            obj.ajaxd(urls[counter]);   
            counter++;
        }else{
            counter = 0;
         }

     },30000);

 });
NEWBIE
  • 355
  • 2
  • 6
  • 22
  • Thanks @NEWBIE . You forgot `if(urls[counter] != undefined) {` and `...},30000); });. If you want to edit your code and thanks again –  Aug 12 '14 at 06:02
0
var urls = ['http://mylink1','http://mylink2','http://mylink3','http://mylink4'],

var request = function(index) {
    $.ajax({
        type : "POST",
        url : urls[index],
        success : function(content) {
            console.log(content);
            if (index + 1 < urls.length) {
                setTimeout(function () {
                    request(index + 1);
                }, 30 * 1000); // 30s
            }
        }
    });
}

request(0);
Junle Li
  • 1,025
  • 1
  • 11
  • 18
0

Use this:

var urls = [ 'http://mylink1','http://mylink2','http://mylink3','http://mylink4'];
urls.reverse();
var interval = setInterval(function(){ 
    if(urls.length == 0){
        clearInterval(interval);
        return;
    }
    var entry = urls.pop();
    console.log(entry);
    ajaxd(entry);
    console.log("merge pana aici");
}, 30000);

Note that this will change your array. It will remove elements from them until it is empty.

EDIT: Based on the comment by Andrei, added a return statement after clearInterval :) Thanks.

pratyush
  • 480
  • 2
  • 10
  • Good answer, but after link4, you got undefined and the loop is over. :) [Salutari din romania :)) ] –  Aug 12 '14 at 07:02