javascript - 使用 Javascript 将单个倒数计时器更改为多个倒数计时器

标签 javascript timer countdowntimer

大家好,我的计时器有问题,单个计时器工作正常,但是当我将其转换为多个计时器时,计时器无法工作有人可以帮助我吗,谢谢

我添加了一个正在运行的计时器片段 提前致谢! :)

P.S 我不能在这部分使用 Jquery,所以它非常具有挑战性。

// value came here $time_end = sprintf('%013.0f', microtime(true)*1000 + 27000000); 30 minuts timer
var countDownDate = "1506509439553";

// Update the count down every 1 second
var x = setInterval(function() {

  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now an the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Output the result in an element with id="demo"
  document.getElementById("timer_1").innerHTML = minutes + ":" + seconds + "";
  //id is dynamic timer_<?php echo get_the_ID();?>

  // If the count down is over, write some text 
  if (distance <= 0) {
    clearInterval(x);
    document.getElementById("timer_1").style.display = "none";
  }
}, 1000);
<div id="timer_1"></div>

最佳答案

只需将现有代码包装到一个方法中,并将元素 id 传递给它即可。

演示

startTimer( "timer_1" );
startTimer( "timer_2" );
startTimer( "timer_3" );

function startTimer(elementId) {
  // value came here $time_end = sprintf('%013.0f', microtime(true)*1000 + 27000000); 30 minuts timer
  var countDownDate = "1506509439553";

  // Update the count down every 1 second
  var x = setInterval(function() {

    // Get todays date and time
    var now = new Date().getTime();

    // Find the distance between now an the count down date
    var distance = countDownDate - now;

    // Time calculations for days, hours, minutes and seconds
    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    // Output the result in an element with id="demo"
    document.getElementById( elementId ).innerHTML = minutes + ":" + seconds + "";
    //id is dynamic timer_<?php echo get_the_ID();?>

    // If the count down is over, write some text 
    if (distance <= 0) {
      clearInterval(x);
      document.getElementById( elementId ).style.display = "none";
    }
  }, 1000);
}
<div id="timer_1"></div>
<div id="timer_2"></div>
<div id="timer_3"></div>

关于javascript - 使用 Javascript 将单个倒数计时器更改为多个倒数计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46442117/

相关文章:

javascript - YUI 3. 以编程方式触发 onSubmit

javascript - 计算子串最快的方法是什么

java - 在 Java 中最推荐使用计时器的方法是什么

java - 在主类中使用定时器

javascript - selenium ElementNotVisibleException 无法捕获隐藏元素

javascript - AngularJS 复选框第一次渲染速度非常慢

.net - 如何每秒更新一次 WPF 绑定(bind)?

javascript - 通过rails-assets-pipeline使用jcountdown时,未捕获类型错误: $(. ..).countdown不是函数```

android - 即使 Activity 停止,计时器也会运行

android - 如何在 CountDownTimer 上添加和删除时间?