I am working on the following code. Why is the interval
happening only one time instead of 3, as expected on let x= 3
?
Basically I need to repeat this loop/interval for 3 times but it is happening only once.
$(function() {
let colors = ['red', 'blue', 'green']
let x = 3;
let interval = 6000;
for (let i = 0; i < x; i++) {
setTimeout(function() {
$('body').css('background-color', colors[i]);
backtoWhite();
}, i * interval)
}
function backtoWhite() {
setTimeout(function() {
$('body').css('background-color', 'white');
}, 3000);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>