尝试在时间到时再次开始倒计时
trying to start a countdown again when time is up
我正在使用 jQuery.countdown,我正在努力做到这一点,以便在倒计时结束后,它会以另一个我想在每个星期五重新开始的日期重新开始。所以它整个星期都在倒计时,当它到达星期五时,它会发出一条消息,然后等待很短的时间,然后重新启动。我知道如何进行第一次倒计时,但我无法重新开始倒计时。不知道如何设置标志变量或在这种情况下如何使用循环。这是一段不起作用的相关代码。
我在全局上下文中设置了 var again = false
(您在我意识到的片段中看不到它)。然后检查它是否为假,如果新日期 < 然后是 str。 str 在函数中更新,说明完成后做什么。在我的原始代码中,我将 str
设置为第一个未来日期并将其作为参数放入 countdown method
中,我只是将 "2015/7/27 23:49:10"
用于测试。我怎样才能重新设置定时器?我想我需要某种循环机制。
if(again == false && new Date() < new Date(str)){
$('#clock').countdown("2015/7/27 23:49:10") .on('update.countdown', function(event) {
var format = '%D days %H hours %M mminutes %S seconds';
$(this).html(event.strftime(format));
again = true
})
.on('finish.countdown', function(event) {
if(again == true){
$(this).html('This offer has expired!');
str = "2015/7/27 23:49:39"
window.setTimeout(function(){again = false},5000)
again = false;
console.log(str)
}
})
}
在您的 setTimeout
中您可以重新初始化它。你有一个叫做 event.elapsed
的东西来检查时间是否已经过去,并且不需要 finish.countdown
。 Example from site
$(document).ready(function(){
var date='2015/07/29 12:29:00';//change this to very near time and elapse and check it
var format='%D days %H hours %M minutes %S seconds';
$('#clock').countdown(date)
.on('update.countdown', function(event){
$(this).html(event.strftime(format));
}).on('finish.countdown',function(event){
var ev=event;
var ctrl=$(this);
$(ctrl).html('This offer has expired!');
window.setTimeout(function(){
$('#clock').countdown('2015/12/20 12:34:56'); //change this date accordingly
},5000)
})
});
我正在使用 jQuery.countdown,我正在努力做到这一点,以便在倒计时结束后,它会以另一个我想在每个星期五重新开始的日期重新开始。所以它整个星期都在倒计时,当它到达星期五时,它会发出一条消息,然后等待很短的时间,然后重新启动。我知道如何进行第一次倒计时,但我无法重新开始倒计时。不知道如何设置标志变量或在这种情况下如何使用循环。这是一段不起作用的相关代码。
我在全局上下文中设置了 var again = false
(您在我意识到的片段中看不到它)。然后检查它是否为假,如果新日期 < 然后是 str。 str 在函数中更新,说明完成后做什么。在我的原始代码中,我将 str
设置为第一个未来日期并将其作为参数放入 countdown method
中,我只是将 "2015/7/27 23:49:10"
用于测试。我怎样才能重新设置定时器?我想我需要某种循环机制。
if(again == false && new Date() < new Date(str)){
$('#clock').countdown("2015/7/27 23:49:10") .on('update.countdown', function(event) {
var format = '%D days %H hours %M mminutes %S seconds';
$(this).html(event.strftime(format));
again = true
})
.on('finish.countdown', function(event) {
if(again == true){
$(this).html('This offer has expired!');
str = "2015/7/27 23:49:39"
window.setTimeout(function(){again = false},5000)
again = false;
console.log(str)
}
})
}
在您的 setTimeout
中您可以重新初始化它。你有一个叫做 event.elapsed
的东西来检查时间是否已经过去,并且不需要 finish.countdown
。 Example from site
$(document).ready(function(){
var date='2015/07/29 12:29:00';//change this to very near time and elapse and check it
var format='%D days %H hours %M minutes %S seconds';
$('#clock').countdown(date)
.on('update.countdown', function(event){
$(this).html(event.strftime(format));
}).on('finish.countdown',function(event){
var ev=event;
var ctrl=$(this);
$(ctrl).html('This offer has expired!');
window.setTimeout(function(){
$('#clock').countdown('2015/12/20 12:34:56'); //change this date accordingly
},5000)
})
});