I have this array:
let learning = [
"HTML",
"CSS",
"JavaScript",
"JQuery",
"Git",
"GitHub",
"Bootstrap",
"Sass",
"JSon"
];
and want to show each of them, one by one, in an span element with id="learning-here"
I tried like this:
for (const iterator of learning) {
setTimeout(writeList(iterator), 1000);
}
function writeList(i) {
$("#learning-here").text(i);
console.log(i);
}
It seems like setTimeout doesn't work as I thought. The last item on the array is shown on the span as soon as I reload the page and the whole array is console-logged.
Also tried with my poor JQuery knowledge like this:
$.each(learning, function(key, value) {
setTimeout(writeList, 1000);
function writeList() {
$("#learning-here").text(value);
}
console.log(value);
});
How could I show one by one array element on the span? What am I doing wrong on my two tries above?
Thank you.
.text()just means you overwrite the same span every time. And your timeouts are all 1 second. So they all fire at the same time and the last one finish will be shown in the span. Try different timeouts to see the span change every second. ( multiply by index ? )