I would like to console.log values between 1 and 5 infinitely with a small delay of few seconds using JavaScript. I am trying to run the following code, but it stops after printing 1.
var z = 0
setTimeout(function() {
if (z==6) {z=0};
z+=1;
console.log(z);
}, 2000);Is it possible to implement this using setTimeout function?
Answer
You are looking for setInterval:
var z=0;
setInterval(function() {
if (z==6) {z=0};
z+=1;
console.log(z);
}, 2000);
No comments:
Post a Comment