Home > Web Front-end > JS Tutorial > body text

How to implement countdown timer in js

下次还敢
Release: 2024-05-01 04:18:19
Original
1000 people have browsed it

There are two methods to implement countdown timers in JavaScript: setInterval(): Create a timer and call the function repeatedly every specified milliseconds. setTimeout(): Call the function only once, delaying the specified time.

How to implement countdown timer in js

Implementation of countdown timer in JS

In JavaScript, there are several ways to implement countdown timer. The following two methods are commonly used:

1. setInterval() method

setInterval() method creates a timer that specifies The number of milliseconds to repeatedly call a function. To implement a countdown timer using the setInterval() method, follow these steps:

  • Define a function to update the countdown. The function should compare the current time with the target time and display the remaining time.
  • Calculate the remaining time in milliseconds.
  • Use the setInterval() method to call the update function every certain number of milliseconds.
  • When the countdown ends, clear the setInterval timer.

2. setTimeout() method

setTimeout() method only calls the function once, delaying the specified time. To implement a countdown timer using the setTimeout() method, follow these steps:

  • Define a recursive function to update the countdown. The function should compare the current time to the target time and display the remaining time.
  • In the function, calculate the number of milliseconds of remaining time.
  • Use the setTimeout() method to call this function after the remaining time.

Sample code (setInterval() method)

<code>function updateCountdown() {
  const targetTime = new Date('2023-12-31');
  const currentTime = new Date();
  const msToTarget = targetTime - currentTime;
  const msToHours = Math.floor(msToTarget / (1000 * 60 * 60));
  const msToMinutes = Math.floor(msToTarget / (1000 * 60)) % 60;
  const msToSeconds = Math.floor(msToTarget / 1000) % 60;
  const countdownDisplay = document.getElementById('countdown');
  countdownDisplay.innerHTML = `${msToHours}:${msToMinutes}:${msToSeconds}`;
  if (msToTarget <= 0) {
    clearInterval(timeoutID);
  }
}

const timeoutID = setInterval(updateCountdown, 1000);</code>
Copy after login

Sample code (setTimeout() method)

<code>function countdown(ms) {
  const targetTime = Date.now() + ms;
  const countdownDisplay = document.getElementById('countdown');
  const update = () => {
    const msRemaining = targetTime - Date.now();
    if (msRemaining <= 0) {
      return;
    }
    const msToHours = Math.floor(msRemaining / (1000 * 60 * 60));
    const msToMinutes = Math.floor(msRemaining / (1000 * 60)) % 60;
    const msToSeconds = Math.floor(msRemaining / 1000) % 60;
    countdownDisplay.innerHTML = `${msToHours}:${msToMinutes}:${msToSeconds}`;
    setTimeout(update, 1000);
  }
  update();
}

countdown(3600000);  // 1 小时</code>
Copy after login

The above is the detailed content of How to implement countdown timer in js. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!