Я создаю таймер обратного отсчета. Пока это мой код.
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 — приостановка setInterval().
Кажется, что в jquery легко создать опцию паузы. Но я понятия не имею, как я могу применить это к моему сценарию. Кто-нибудь может мне помочь.
pause
в значение true или false - ничего особенного в идее приостановки таймера. Если вы знаете, как добавлять обработчики событий без jQuery, ответ уже есть. - person kshetline   schedule 01.05.2018