Answer:
var count = 0;
var counterElement = document.getElementById("counter");
counterElement.innerHTML = count;
var interval = setInterval(function () {
count++;
counterElement.innerHTML = count;
if (count === 5) {
clearTimeout(interval);
}
}, 300);
Explanation:
- Initialize the count and get access to the counter element using the id.
- Assign the value of count to counterElement document.
- Use the setInterval function to set the timer and increases the count by 1.
- Check if the value of count is equal to 5, then call the built-in clearTimeout function by passing it the interval variable.