Javascript - set spacing for each array value. setinterval, array, foreach
I am trying to get each link multiple intervals. For example: I get the first link, wait 30 seconds, the next link, wait 30 seconds for 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 that after 30 seconds I get all the links. Not the first value, wait 30 seconds, next value, etc.
+3
user3863229
source
to share
7 replies
EDIT Updated the code to repeat the loop.
(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
+3
source to share
You are doing it wrong, try to create a counter every 30 seconds. 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);
});
0
source to share
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);
0
source to share
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 items from them until it becomes empty.
EDIT: Based on Andrey's comment, added a return statement after clearInterval :) Thanks.
0
source to share
If you still want to use forEach to encode the array, you can use it with setTimeout, but 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;
});
0
source to share