EN
TypeScript - 1 minute countdown timer
0
points
In this article, we would like to show you how to create a one-minute countdown timer in TypeScript.
Practical example
In the example below, we use setTimeout() method to execute a function once the timer expires.
As setTimeout() arguments we pass:
makeIteration()function that printssecondsand clears the console from the previous iteration,1000milliseconds to makesetTimeout()count every second.
let seconds: number = 60;
const makeIteration = (): void => {
console.clear();
if (seconds > 0) {
console.log(seconds);
setTimeout(makeIteration, 1000); // 1 second waiting
}
seconds -= 1;
};
setTimeout(makeIteration, 1000); // 1 second waiting