How to start the cycle over again?

Wrote code which reads data from the array and displays them one by one with a half second delay. But after all data is read, I need to re-start the cycle. The idea was to put it all in while (true), but it was a bad idea. Tried to Google, but I was constantly thrown to an article on the closures. There really need to use closures? If so, how

<p>Open <b class="slide-words">castle</b></p>

var keys = ['lock', 'car', 'safe'];

for (var i = 0; i < keys.length; i++) {
slideTime(i)
}

function slideTime(j) {
 setTimeout(function() {
 var slideWords = document.querySelector('.slide-words');
 slideWords.textContent = keys[j];
 }, 1500 * j);
}
March 20th 20 at 11:39
2 answers
March 20th 20 at 11:41
Solution
Don't have any "new" or cycles:

let index = -1;
setInterval(() => {
 index = (index + 1) % keys.length;
 document.querySelector('.slide-words').textContent = keys[index];
}, 1500);
Wow. This is how I understand the functionality of ES. Not yet got to study them. Thank you! - Caroline_K commented on March 20th 20 at 11:44
@Ethel_Hackett43, under the "ES" you mean ES6/7/...? No, nothing special about it, replace let to var, switch function on normal, everything will work exactly the same. - Delpha_Berni commented on March 20th 20 at 11:47
@Ethel_Hackett43, In your solution you use setTimeout, setInterval from the same Opera, ES nothing to do with.
https://learn.javascript.ru/settimeout-setinterval - adolphus commented on March 20th 20 at 11:50
@Watson.Dool, Yes it is about ES6/7. Then it is setInterval that allows you to produce. Not delved into it deeply, so as to learn how to operate the browser has not yet reached. - Caroline_K commented on March 20th 20 at 11:53
@domenic, Thanks! - Caroline_K commented on March 20th 20 at 11:56
March 20th 20 at 11:43
Solution
using recursion can

Find more questions by tags JavaScript