I want the script call to wait for 2 seconds after every 5 pages.
My requirements are that the for
loop should wait for 2 seconds when pageData.Number
reaches page 5. So basically, the loop should wait for 2 seconds after every 5 pages.
Not sure how to check if the page reaches page 5 and wait for 2 seconds.
for (var i = 1; i <= pageData.Number; i++) {
var pageRequest = $.getJSON(URL, function (data) {
if (i % 5 == 0) {
sleep(5000);
}
/* .... */
}).fail(function () {
errorPage.push(i);
});
}
// from https://www.sitepoint.com/delay-sleep-pause-wait/
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
2
Answers
I think you need something like this. It uses javascript remainder operator and a custom sleep function.
Here’s how you could do it – with async/await and without jQuery.