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.
In the example below, we use setTimeout()
method to execute a function once the timer expires.
As setTimeout()
arguments we pass:
makeIteration()
function that printsseconds
and clears the console from the previous iteration,1000
milliseconds to makesetTimeout()
count every second.
xxxxxxxxxx
1
let seconds: number = 60;
2
3
const makeIteration = (): void => {
4
console.clear();
5
if (seconds > 0) {
6
console.log(seconds);
7
setTimeout(makeIteration, 1000); // 1 second waiting
8
}
9
seconds -= 1;
10
};
11
12
setTimeout(makeIteration, 1000); // 1 second waiting