gpt4 book ai didi

javascript - Flipclock js倒计时1小时无需重置

转载 作者:行者123 更新时间:2023-11-29 19:23:58 37 4
gpt4 key购买 nike

我试图在不重置刷新时间的情况下进行倒计时。我只需要倒计时 1 小时,但我不希望它在刷新时重置。从服务器获取当前时间和过期时间不起作用,因为我想通过单击重置该计时器。这是我当前的 js 代码:

<script>
var clock;


clock = $('.clock').FlipClock({
clockFace: 'HourlyCounter',
autoStart: false,
callbacks: {
stop: function() {
$('.message').html('The clock has stopped!')
},
}
});
clock.setTime(3600);
clock.setCountdown(true);
</script>

最佳答案

是的,您可以使用 flipclock 在您的示例中执行此操作 jquery-cookie 看看 1 minute countdown Example .

flipclockinit() 函数中将结束日期存储在 cookie 中,使用 clock.setTime() 初始化计时器,启用倒计时clock.setCountdown(true) 并通过 clock.start() 启动它。

现在,如果用户刷新页面,我们将计时器设置为当前日期和结束日期之间的差异,这样计数器就可以正常继续倒计时:

var counter = $.cookie('endDate')-currentDate;
clock.setTime(counter);

点击 reset 按钮将通过删除 cookie endDate 并初始化倒计时功能来重置计数器。

HTML :

<div class="clock" style="margin:2em;"></div>
<div class="message"></div>
<button id='reset'>Reset</button>

Js:

//ready function
$(function(){

countDown = function(){
var currentDate = Math.round(new Date() / 1000);

var clock = $('.clock').FlipClock({
countdown: true,
callbacks: {
init: function() {
//store end date If it's not yet in cookies
if(!$.cookie('endDate')){
// end date = current date + 1 minutes
var endDate = Date.now() + 1*60*1000;

// store end date in cookies
$.cookie('endDate', Math.round(endDate / 1000));
}
},
stop: function() {
$('.message').html('The clock has stopped!');
},
}
});

/* counter will be at first 1 min if the user refresh the page the counter will
be the difference between current and end Date, so like this counter can
continue the countdown normally in case of refresh. */
var counter = $.cookie('endDate')-currentDate;

clock.setTime(counter);
clock.setCountdown(true);

clock.start();
}

//reset button
$('#reset').click(function(){
$.removeCookie('endDate'); //removing end date from cookies
countDown(); //launch countdown function
});

//Lanching count down on ready
countDown();
});

在您的情况下(1 小时),您只需将这一行中的 1 替换为 60 即可:

var endDate = Date.now() + 1*60*1000; // For you .... + 60*60*1000

关于javascript - Flipclock js倒计时1小时无需重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31670979/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com