I'm having trouble Grok'ing a simple JavaScript for loop.
Here is an example:
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (i = 1; i <= 5; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
for(const value of arr) {
document.write(value);
document.write("<br />");
}
}, i * 1000);
}
What I'm trying to do is pause after it prints the first value, but instead it runs the entire For loop then pauses. I think I'm trying to do something with for loops that just won't work, but I would like to know why.
Any help would be appreciated.
Plunkr here: https://plnkr.co/edit/tnmFrIRTDJI8T294Qh4z?p=preview
The example Javascript, setTimeout loops? didn't help me figure it out. I still got the concept wrong as George Pantazes points out.