jQuery Countdown - 重置计时器

发布于 2024-12-08 19:01:23 字数 821 浏览 0 评论 0原文

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

我的代码目前如下所示:

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

目前,即使调用了 doCountdown 函数 onExpiry(使用 console.log 进行了测试),它仍停留在 00:00:00 code> 并且它肯定会调用它)。

是因为javascript基于页面加载时间然后存储它吗?

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();
});

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.

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).

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

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

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

发布评论

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

评论(1

梦一生花开无言 2024-12-15 19:01:23

原因是您的 nextNoon 创建错误计算了上午 11:30 到下午 12:00 之间的时间。对于这半个小时的时间段,if() 将计算为 false,因此它将时间设置为当天的上午 11:30。然而我们已经过了那个时间了,因为我们是在上午 11:30 到中午 12 点之间。所以倒计时将归零。

您需要执行以下操作:

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);

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.

You need to do as follows:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文