I need to set a delay in the execution of a for loop in JavaScript.
Here is my code:
function myFunction1() {
var list = document.getElementById('SomeList');
var items = list.getElementsByTagName('li');
for (var i = 0; i < items.length; ++i) {
setTimeout(delayLoop, 1500);
}
}
function delayLoop() {
alert('Hello');
}
After adding the "alert('Hello')" code to the delayLoop function, I noticed that the setTimeout function only displays the alert box after the execution of myFunction1().
How can I use setTimeout to slow down each loop through the collection of items to 1500ms?