解决基于 javascript 的倒计时中的负值

发布于 2024-11-14 09:32:36 字数 1017 浏览 5 评论 0原文

我正在创建一个倒计时来计算我打算观看的 2012 年欧洲杯比赛之间的时间。 我已经提供了它的工作版本,但我不明白为什么它有时会给我负值。 我认为这与我使用 getTime() 方法编写它的方式有关。 这是我的代码,你们能帮我找出解决这些负值的方法吗? 预先非常感谢您。

<body onload="timeto2012()">
<script type="text/javascript">
 function timeto2012() {
 var euro2012 = new Date(2012, 5, 10, 20, 45);
 var euro2012ms = euro2012.getTime();
var now = new Date();
var nowms = now.getTime();

var diff = euro2012ms - nowms;

var seconds = 1000;
var minutes = seconds*60;
var hours = minutes*60;
var days = hours*24;
var years = days*365;

var ddays = diff/days;
var dhours = (ddays - Math.round(ddays,1))*24;
var dminutes = (dhours - Math.round(dhours))*60;
var dseconds = (dminutes - Math.round(dminutes))*60;

document.getElementById("time").innerHTML='' + Math.round(ddays,1) +' days '+ Math.round(dhours,1) +' hours '+ Math.round(dminutes,1) +' minutes '+ Math.round(dseconds,1) + ' seconds remaining';

}

t=setInterval(timeto2012,500);

</script>   
<div id="time"></div>
</body>

I am creating a countdown to count the time between a match of the euro2012 that I intend to watch.
I've come with a working version of it but I don't understand why it gives me sometimes negative values.
I think it has to do with the way I wrote it, using the getTime() method.
Here is my code, could you guys help me find out to solve those negative values?
Thank you very much in advance.

<body onload="timeto2012()">
<script type="text/javascript">
 function timeto2012() {
 var euro2012 = new Date(2012, 5, 10, 20, 45);
 var euro2012ms = euro2012.getTime();
var now = new Date();
var nowms = now.getTime();

var diff = euro2012ms - nowms;

var seconds = 1000;
var minutes = seconds*60;
var hours = minutes*60;
var days = hours*24;
var years = days*365;

var ddays = diff/days;
var dhours = (ddays - Math.round(ddays,1))*24;
var dminutes = (dhours - Math.round(dhours))*60;
var dseconds = (dminutes - Math.round(dminutes))*60;

document.getElementById("time").innerHTML='' + Math.round(ddays,1) +' days '+ Math.round(dhours,1) +' hours '+ Math.round(dminutes,1) +' minutes '+ Math.round(dseconds,1) + ' seconds remaining';

}

t=setInterval(timeto2012,500);

</script>   
<div id="time"></div>
</body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘初心 2024-11-21 09:32:36

KOGI 可以解决您的问题:您应该使用 Math.floor 而不是 Math.round

当还剩 x 分钟 30 - 59 秒时,(舍入完成后,x - Math.round(x)) 相当于 (x - (x + 1))

var ddays = diff/days;
var dhours = (ddays - Math.floor(ddays))*24;
var dminutes = (dhours - Math.floor(dhours))*60;
var dseconds = (dminutes - Math.floor(dminutes))*60;

小提琴:http://jsfiddle.net/YHktx/3/

KOGI has the answer to your problem: You should use Math.floor instead of Math.round:

When there's x minutes and 30 - 59 seconds left, the (x - Math.round(x)) would be equivalent to (x - (x + 1)) after the rounding was done.

var ddays = diff/days;
var dhours = (ddays - Math.floor(ddays))*24;
var dminutes = (dhours - Math.floor(dhours))*60;
var dseconds = (dminutes - Math.floor(dminutes))*60;

Fiddle: http://jsfiddle.net/YHktx/3/

哎呦我呸! 2024-11-21 09:32:36

这里有一些更好的计算给你(来自 Aleksi Yrttiaho 的 jsFiddle)

var seconds = 1000;
var minutes = seconds*60;
var hours = minutes*60;
var days = hours*24;
var years = days*365;

var ddays = Math.floor( diff/days );
var dhours = Math.floor((diff % days) / hours );
var dminutes = Math.floor(((diff % days) % hours) / minutes );
var dseconds = Math.floor((((diff % days) % hours) % minutes) / seconds );

Here are some better calculations for you (derived from Aleksi Yrttiaho's jsFiddle)

var seconds = 1000;
var minutes = seconds*60;
var hours = minutes*60;
var days = hours*24;
var years = days*365;

var ddays = Math.floor( diff/days );
var dhours = Math.floor((diff % days) / hours );
var dminutes = Math.floor(((diff % days) % hours) / minutes );
var dseconds = Math.floor((((diff % days) % hours) % minutes) / seconds );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文