To create a countdown timer in JavaScript, you can use the setInterval
method to repeatedly execute a function that updates the remaining time and displays it.
Here's an example code snippet:
index.tsx817 chars25 lines
In this example, we set the countdown date to 10 minutes from now using the getTime
method and the 10 * 60 * 1000
value, which represents 10 minutes in milliseconds. Then, we use setInterval
to execute a function every second that calculates the remaining time by subtracting the current time from the countdown date. We also calculate the minutes and seconds using the floor
method and update a HTML element with the remaining time using the innerHTML
property. Finally, if the remaining time is zero or negative, we clear the interval and display a message.
gistlibby LogSnag