如何使用Javascript在同一页面中创建多个倒数计时器? [英] How To Create Multiple CountDown Timers In The Same Page Using Javascript?

查看:31
本文介绍了如何使用Javascript在同一页面中创建多个倒数计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让这段代码成功运行,但没有任何运气.我正在尝试创建一个 javascript 函数,该函数将在同一页面上对多个日期进行倒计时.我的 JavaScript 函数如下:

I've been trying to get this piece of code to work successfully without any luck. Im trying to create a javascript function that will countdown multiple dates on the same page. My JavaScript function is as followed:

 <script>
   function getTimeRemaining(endtime){
     var deadline = new Date(endtime).getTime(); 
     var x = setInterval(function() { 
       var now = Math.floor(Date.now() / 1000); 
       var t = deadline - now; 
       var days = Math.floor(t / (1000 * 60 * 60 * 24)); 
       var hours = Math.floor((t % (1000 * 60 * 60 * 24))/(1000 * 60 * 60)); 
       var minutes = Math.floor((t % (1000 * 60 * 60)) / (1000 * 60)); 
       var seconds = Math.floor((t % (1000 * 60)) / 1000); 
       document.getElementById("trip_" + endtime).innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s"; 
       if (t < 0) { 
           clearInterval(x); 
           document.getElementById("trip_" + endtime).innerHTML = "expired"; 
       } 
     }, 1);
   }
 </script>

结束时间以 unix 时间戳的形式提供.如果时间已过,它会正确显示.但如果剩余时间,它会显示 0d0h0m0s 而没有任何更新.只是为了好玩,我将 t 变量添加到倒计时的末尾,它会倒计时剩余的秒数,但不会在倒计时本身上显示它.我猜我的天数、小时数、分钟数和秒数变量有问题.我没有使用毫秒,我认为我使用的数学是假设我这样做的.有谁知道我能做些什么来解决我的问题?我不太熟悉 javascript.提前致谢!

the end time is provided as a unix timestamp. If the time has expired it displays it correctly. But if time is remaining it displays 0d0h0m0s without any updates. Just for fun, I added the t variable to the end of the countdown and it counts downs the remaining seconds but doesn't show it on the countdown itself. Im guessing there is something wrong with my days, hours, minutes, and seconds variables. I am not using milliseconds and I think the math im using is assuming I do. Does anyone know what I could do to solve my problem? Im not to well versed with javascript. Thanks in advance!

推荐答案

可能是这样的:

function TimeRemaining(){
   var els = document.querySelectorAll('[id^="trip_"]');
   for (var i=0; i<els.length; i++) {
   	 var el_id = els[i].getAttribute('id');
   	 var end_time = el_id.split('_')[1];
   	 var deadline = new Date(end_time);
     var now = new Date();
     var t = Math.floor(deadline.getTime() - now.getTime());
     var days = Math.floor(t / (1000 * 60 * 60 * 24));
     var hours = Math.floor((t % (1000 * 60 * 60 * 24))/(1000 * 60 * 60));
     var minutes = Math.floor((t % (1000 * 60 * 60)) / (1000 * 60));
     var seconds = Math.floor((t % (1000 * 60)) / 1000);
     if (t < 0) {
        document.getElementById("trip_" + end_time).innerHTML = 'EXPIRED';
     }else{
     	document.getElementById("trip_" + end_time).innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s";
     }
   }
}

function StartTimeRemaining(){
    TimeRemaining();
	setInterval(function(){
		TimeRemaining();
	}, 1000)
}


StartTimeRemaining();

<div id="trip_2019-12-31"></div>
<div id="trip_2019-11-01 01:02:12"></div>
 <div id="trip_2019-01-01"></div>

这篇关于如何使用Javascript在同一页面中创建多个倒数计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆