[英]Trying to pause and resume a timer with same button
I am having a little trouble with this code. 我在使用此代码时遇到了一些麻烦。 I am currently have a button that when clicked starts a timer. 我目前有一个按钮,单击该按钮将启动计时器。 It then also changes the icon of the button from a start icon to a stop icon. 然后,它还将按钮的图标从开始图标更改为停止图标。 I would like to stop this timer when the button is clicked again and have it switch back the icon from start to stop. 我想在再次单击该按钮时停止该计时器,并使其从开始位置切换到停止位置。 I have been stuck for awhile and tried many things but cant get it to work and would appreciate any help. 我已经被卡住了一段时间,尝试了很多事情,但是无法正常工作,将不胜感激。 I am sure its a simple issue I am just new to javascript. 我确信这是一个简单的问题,我是javascript新手。 Thanks. 谢谢。
(function () {
"use strict";
var minutesLabel = document.getElementById('t1-min'),
secondsLabel = document.getElementById('t1-sec'),
resetButton = document.getElementById('t1-reset'),
startButton = document.getElementById('t1-play'),
timer = null;
function getTimeRemaining(endtime) {
var t = Date.parse(endtime) - Date.parse(new Date());
var seconds = Math.floor((t / 1000) % 60);
var minutes = Math.floor((t / 1000 / 60) % 60);
return {
'total': t,
'minutes': minutes,
'seconds': seconds
};
}
function initializeClock(endtime) {
var timeinterval = setInterval(function () {
var t = getTimeRemaining(endtime);
if (t.total <= 0) {
clearInterval(timeinterval);
}
minutesLabel.innerHTML = t.minutes;
secondsLabel.innerHTML = pad(t.seconds);
}, 1000);
}
startButton.onclick = function runClock() {
var timeInMinutes = minutesLabel.innerHTML,
currentTime = Date.parse(new Date()),
deadline = new Date(currentTime + timeInMinutes * 60 * 1000);
initializeClock(deadline);
startButton.innerHTML = ('<i class="far fa-stop-circle fa-2x float-right">');
}
function pad(val) {
var valString = val + "";
if (valString.length < 2) {
return "0" + valString;
} else {
return valString;
}
}
})(); })();
Firstly, make sure that the event listener in placed correctly on the startButton
element by adding a function on addEventLister
method like that: 首先,通过在addEventLister
方法上添加如下函数,确保将事件侦听器正确放置在startButton
元素上:
startButton.addEventListener("click", function() {
//code goes here
});
Then initialize the timeinterval
variable in the global scope of the code so that it can be accessed from all functions, and add a const
in which you can save the initial text of the startButton
: 然后在代码的全局范围内初始化timeinterval
变量,以便可以从所有函数访问它,并添加一个const
,您可以在其中保存startButton
的初始文本:
(function () {
"use strict";
const START_BUTTON_INITIAL_TEXT = "Start"; // initial constant text of the start button
var minutesLabel = document.getElementById('t1-min'),
secondsLabel = document.getElementById('t1-sec'),
resetButton = document.getElementById('t1-reset'),
startButton = document.getElementById('t1-play'),
timer = null,
timeinterval; // declared in the global scope
Then add an id
equal to t1-stop
for the stopButton
icon element to make sure that it can be accurately accessed by the Javascript file from the DOM: 然后为stopButton
图标元素添加一个等于t1-stop
的id
,以确保Javascript文件可以从DOM正确访问它:
startButton.innerHTML = ('<i id="t1-stop" class="far fa-stop-circle fa-2x float-right">');
lastly add this if statement in addEventListener
function of startButton
to check whether a stop button exists in the DOM when the startButton
is clicked: 最后在startButton
addEventListener
函数中添加以下if语句,以检查单击startButton
时DOM中是否存在停止按钮:
startButton.addEventListener("click", function() {
if (document.getElementById("t1-stop") != null) {
clearInterval(timeinterval);
var stopButton = document.getElementById("t1-stop");
startButton.removeChild(stopButton);
startButton.innerHTML = START_BUTTON_INITIAL_TEXT;
}
The above code stops the timeinterval
and removes the stopButton
from the DOM then replaces the innerHTML
of startButton
with the initial text set as a constant at the top variable declaration. 上面的代码停止timeinterval
并从DOM中删除stopButton
,然后将startButton
的innerHTML
替换为在顶部变量声明中设置为常量的初始文本。
All in all, your event listener function for the startButton
should look something like that: 总而言之,您对startButton
事件监听器功能应如下所示:
startButton.addEventListener("click", function() {
if (document.getElementById("t1-stop") != null) {
clearInterval(timeinterval);
var stopButton = document.getElementById("t1-stop");
startButton.removeChild(stopButton);
startButton.innerHTML = START_BUTTON_INITIAL_TEXT;
return;
}
var timeInMinutes = minutesLabel.innerHTML,
currentTime = Date.parse(new Date()),
deadline = new Date(currentTime + timeInMinutes * 60 * 1000);
initializeClock(deadline);
startButton.innerHTML = ('<i id="t1-stop" class="far fa-stop-circle fa-2x float-right">');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.