jQuery Countdown-重置计时器 [英] jQuery Countdown - reset timer

查看:397
本文介绍了jQuery Countdown-重置计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery Countdown 插件并进行快速查询.

I'm using the jQuery Countdown plugin and have a quick query.

我的代码当前如下所示:

My code currently looks like this:

function doCountdown(){
    var nextNoon = new Date();
    if (nextNoon.getHours()>=12){ nextNoon.setDate(nextNoon.getDate()+1); }
    nextNoon.setHours(11,30,0,0);

    $('h3 .timer strong').countdown({until: nextNoon, compact: true, 
        description: '',  onExpiry: function(){doCountdown()}});
}

$(window).load(function(){
     doCountdown();
});

因此,基本上,它递减计数直到下一个11:30AM.但是,我需要它在达到11:30AM时重置计数器,因此它将在计时器上自动转到23:59:59.

So basically, it counts down untill the next 11:30AM. However I need it to reset the counter when it reaches 11:30AM, so it will automatically go to 23:59:59 on the timer.

当前,即使doCountdown函数被称为onExpiry(已通过console.log测试并且肯定会调用它),它仍停留在00:00:00上.

Currently it just sticks at 00:00:00 even though the doCountdown function is called onExpiry (tested with console.log and it definitely calls it).

是因为javascript会根据页面加载时间来进行计时,然后再进行存储吗?

Is it because javascript bases the time off page load and then stores it?

推荐答案

原因是因为您的nextNoon创作在11:30 am到12:00 pm之间的时间计算错误.在这半小时内,if()的计算结果为false,因此它将时间设置为当天的11:30 am.但是,由于我们处于上午11:30至中午12点之间,所以我们已经过去了.因此,倒数将为零.

The reason is because your nextNoon creation miscalculates for times between 11:30am and 12:00pm. For that half an hour period, the if() will evaluate to false, so it will set the time as 11:30am of the current day. However we've already passed that time, since we're between 11:30am and 12noon. So the countdown will just go to zero.

您需要执行以下操作:

var todaysNoon = new Date(), nextNoon = new Date();
todaysNoon.setHours(11,30,0,0);
if (todaysNoon <= nextNoon){ nextNoon.setDate(nextNoon.getDate()+1); }
nextNoon.setHours(11,30,0,0);

这篇关于jQuery Countdown-重置计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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