setTimeout runs only once?
Yes, setTimeout only runs once. You want setInterval
. This function also returns an ID you can use to cancel the interval. For example:
const slideInterval = setInterval(slide, 3000);
// later...
clearInterval(slideInterval);
setTimeout
should only run once. You're looking for setInterval
.
var loop_handle = setInterval(slide, 3000);
Also, the second argument should be a number, not a string. When the function call doesn't require any arguments, it's better to reference to the function instead of using a string. A string would be converted to a function. This function will be executed within the scope of the window.
setInterval("slide()", 3000);
//becomes
setInterval(Function("slide();"), 3000);