https://www.edureka.co/community/85782/how-do-i-add-a-delay-in-a-javascript-loop#:~:text=The%20setTimeout()%20function%20is,in%20succession%20without%20any%20delay.
var i = 15; // set your counter to 1
function myLoop() { // create a loop function
setTimeout(function() { // call a 3s setTimeout when the loop is called
console.log('hello'); // your code here
i++; // increment the counter
function download(source){
const fileName = source.split('/').pop();
var el = document.createElement("a");
el.setAttribute("href", source);
el.setAttribute("download", fileName);
document.body.appendChild(el);
el.click();
el.remove();
}
if (i < 150) { // if the counter < 10, call the loop function
myLoop(); // .. again which will trigger another
var url='https://asmitapublication.com/wp-content/uploads/real3d-flipbook/flipbook_256/';
var file= i +'.jpg'
let source = url + file;
console.log(`${source}`);
download(source);
} // .. setTimeout()
}, 500)
}
myLoop();
The setTimeout() function is non-blocking and will return immediately. Therefore your loop will iterate very quickly and it will initiate 3-second timeout triggers one after the other in quick succession. That is why your first alerts pops up after 3 seconds, and all the rest follow in succession without any delay.
You may want to use something like this instead:
var i = 1; // set your counter to 1 function myLoop() { // create a loop function setTimeout(function() { // call a 3s setTimeout when the loop is called console.log('hello'); // your code here i++; // increment the counter if (i < 10) { // if the counter < 10, call the loop function myLoop(); // .. again which will trigger another } // .. setTimeout() }, 3000) } myLoop(); // start the loop
Hope it work!!
Thank you!
https://stackoverflow.com/questions/17527713/force-browser-to-download-image-files-on-click
var link = document.createElement('a');
link.href = 'https://asmitapublication.com/wp-content/uploads/real3d-flipbook/flipbook_256/11.jpg';
link.download = 'Download.jpg';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
function download(source){
const fileName = source.split('/').pop();
var el = document.createElement("a");
el.setAttribute("href", source);
el.setAttribute("download", fileName);
document.body.appendChild(el);
el.click();
el.remove();
}
https://www.codegrepper.com/code-examples/javascript/javascript+download+image+from+url
No comments:
Post a Comment