Using Javascript I am trying to take a string of text and have it broken down into individual words and then have them displayed consecutively separated by a set delay. I have looked at other 'set timeout in for loop' stack overflow answers but can't figure it out. The code below properly separates the string into an array of words but then loops to displaying the last word without delay.
var string = "Display this line of text as individual words in two second intervals!";
var delay = 2000;
function myFunction() {
var array = string.split(' ');
for (var i = 0; i < array.length; i++) {
setTimeout(function() {
document.getElementById("textbox").innerHTML = array[i];
}, delay);
}
}
<button onclick="myFunction()">Run</button>
<div id="textbox"></div>