I am trying to download files from some website say 'website.com'. I have all the unique ids in an array
.
This is the code that I have written:
for(var i=0;i<ids.length;i++) {
var timeInterval = 1000;
(function(i,timeInterval){
setTimeout(function(){
var link = document.createElement('a');
link.href= "http://website.com/download/"+ids[i];
var e = document.createEvent('MouseEvents');
e.initEvent('click', true, true);
link.dispatchEvent(e);
console.log(ids[i]);
console.log(timeInterval);
}, timeInterval);
timeInterval = (i+1)*1000;
})(i,timeInterval);
}
Problem with this is that it downloads the file corresponding to the last element of the array ids, not all of them.
How can I make the loop work for each and every element in ids?
PS: I have tried out various solutions given in different related questions, but none could bring desired results, so I had to post it as a different question.