在事件而不是页面加载后启动 jQuery 计时器



我正在从以下页面运行jquery计时器:

http://hilios.github.io/jQuery.countdown/documentation.html#controls

代码如下:

//Timer
var hms = '00:45:00';   // Time Input
var a = hms.split(':'); 
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); 
//console.log(seconds);
var remainSeconds = new Date().getTime() + (seconds*1000);
$('#spanTimer').countdown(remainSeconds)
.on('update.countdown', function(event) {
var $this = $(this);
if (event.elapsed) {
$this.html(event.strftime('After end: <span>%H:%M:%S</span>'));
} else {
$this.html(event.strftime('Time Remaining: <span>%H:%M:%S</span>'));
}
})
//finish.countdown callback, to hide the countdown and other actions
.on('finish.countdown', function(){
$(this).hide();
$("#test").show();
});

计时器按如下方式加载到div 上:

<div id="spanTimer"></div>

div 的可见性在使用 css 的页面加载时隐藏:

#spanTimer{
display:none;
}

可见性根据事件切换,如下所示:

$('#spanTimer').show();

问题是计时器从页面加载开始运行,而不是基于事件。我想在显示div 时启动计时器 #spantimer。这怎么可能??

在插件页面中,我看到了延迟初始化的选项。但是由于我是javascript新手,因此我无法理解它的用法以及如何实现它。

向Javascript专家寻求帮助。谢谢你!

我们看不到触发显示spanTimer的是什么 - 我的意思是是什么事件或操作触发了.show()。 但不管是什么,你都可以同时启动你的倒计时,就像这样:

$('#spanTimer').show().countdown(remainSeconds).on ...

最新更新