【发布时间】:2018-05-01 07:50:59
【问题描述】:
我正在创建一个倒数计时器。所以这是我目前的代码。
function countdownTimeStart() {
var el = document.getElementById('demo');
var cancel = document.getElementById('cancel');
/* Start count the time in timer panel */
var time = document.getElementById("picker-dates").value;
time = time.split(':');
var x = setInterval(function () {
// set hours, minutes and seconds, decrease seconds
var hours = time[0];
var minutes = time[1];
var seconds = time[2]--;
console.log(time);
// create a function to handle the cancel
function cancelCountdown(){
el.innerHTML = "00:00:00";
clearInterval(x);
}
// attach listener to cancel if cancel button is clicked
cancel.addEventListener( 'click', cancelCountdown);
// if seconds are negative, set them to 59 and reduce minutes
if (time[2] == -1) {
time[1]--;
time[2] = 59
}
// if minutes are negative, set them to 59 and reduce hours
if (time[1] == -1) {
time[0]--;
time[1] = 59
}
// Output the result in an element with id="demo"
if( seconds == 0 && minutes == 0 && hours == 0 ){
clearInterval(x);
el.innerHTML = "00:00:00";
} else if (seconds < 10) {
el.innerHTML = hours + ": " + minutes + ": " + "0" + seconds + " ";
} else {
el.innerHTML = hours + ": " + minutes + ": " + seconds + " ";
}
}, 1000);}
所以我想为此创建一个暂停按钮。我提到了类似的问题,例如Javascript - Pausing setInterval()。
在 jquery 中创建暂停选项似乎很容易。但我不知道如何将它应用到我的脚本中。谁能帮帮我。
【问题讨论】:
-
保存间隔开始的时间,点击按钮取消间隔,计算应该有的时间间隔,开始新的间隔直到计算的时间结束
-
能否请您指出方法作为答案。谢谢。
-
jQuery 在您链接到的答案中的唯一意义是将事件处理程序添加到几个按钮,这些按钮将
pause标志设置为真或假——这个想法没有什么特别之处暂停计时器。只要你知道如何在没有 jQuery 的情况下添加事件处理程序,答案就在那里。
标签: javascript countdown pause