You can use the setInterval function in JavaScript to create a countdown timer. Here's an example of how you can do it using jQuery:

HTML:

<div id='timer'>1:10</div>

JavaScript:

$(document).ready(function() {
  var time = 70; // 1 minute and 10 seconds
  var interval;

  function startTimer() {
    interval = setInterval(function() {
      var minutes = Math.floor(time / 60);
      var seconds = time % 60;

      // Display the time in the HTML element
      $('#timer').text(minutes + ':' + seconds);

      // Decrease the time by 1 second
      time--;

      // Check if the timer has reached zero
      if (time < 0) {
        clearInterval(interval);
        // Trigger the event
        $('#timer').trigger('countdownFinished');
      }
    }, 1000); // Run the code every second (1000 milliseconds)
  }

  // Start the timer
  startTimer();

  // Event handler for the countdownFinished event
  $('#timer').on('countdownFinished', function() {
    // Do something when the countdown reaches zero
    alert('Countdown finished!');
  });
});

In this example, the timer starts at 1 minute and 10 seconds (70 seconds) and counts down to zero. Once it reaches zero, an event named countdownFinished is triggered, and you can perform any desired action inside the event handler.

jQuery Countdown Timer: Minutes and Seconds with Event Trigger

原文地址: https://www.cveoy.top/t/topic/qyfw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录