var interval = window.setInterval(animate, 500);
var i = 5;
function animate() {
if (i > 1) {
i--;
console.log(i);
} else {
window.clearInterval(interval);
}
}
animate();
This block of javascript code initializes the variable i
with a value of 5 and logs numbers from 5 to 1. View a live demo on this fiddle link.
In an attempt to pass the initial number as an argument to the animate()
function, some modifications were made:
var interval = window.setInterval(animate, 500);
var i;
function animate(i) {
if (i > 1) {
i--;
console.log(i);
} else {
window.clearInterval(interval);
}
}
animate(10);
Unfortunately, this revised version only displays the number 9 instead of the expected sequence from 10 to 1. Check out the demonstration at this fiddle link.
If anyone has insights on what might be causing this issue, your input would be greatly appreciated.