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..
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..
Share Improve this question edited Aug 12, 2014 at 5:35 Husen 9351 gold badge6 silver badges18 bronze badges asked Aug 12, 2014 at 5:08 user3863229user38632297 Answers
Reset to default 3EDIT 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
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);
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;
});
In your closure you use a for loop to request all the items at once.
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);
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);
});
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 ment by Andrei, added a return statement after clearInterval :) Thanks.