I have two example code.
The two are same purpose, to increase the variable count
by 1, and print the value.
But the first example using setInterval
, and the second example using setTimeout
.
var count = 0;
// First example.
var time = setInterval(function() {
document.body.innerHTML = count;
count++;
}, 1000);
// Second example.
var time = setTimeout(function() {
document.body.innerHTML = count;
count++;
}, 1000);
Why in the first example increases the value of variable and prints its value Continuously, but in the second example increases the value of variable by 1 and prints its value once Then stops and does not continue?
I ask this question, because I've seen some examples work continuously using setTimeout
also.