-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfetchURLsWithDelay.js
31 lines (28 loc) · 994 Bytes
/
fetchURLsWithDelay.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function fetchURLsWithDelay(urls) {
let index = 0;
function fetchNext() {
if (index < urls.length) {
fetch(urls[index])
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log('Data fetched:', data);
index++;
setTimeout(fetchNext, 1000); // 1000 milliseconds = 1 second
})
.catch(error => {
console.error('Error fetching data:', error);
index++;
setTimeout(fetchNext, 1000); // Move to next URL even if there's an error
});
}
}
fetchNext();
}
// Example usage:
const urls = ['https://example.com/url1', 'https://example.com/url2'];
fetchURLsWithDelay(urls);