[英]Using addEventListener and removeEventListener twice on a single button in JavaScript
I am trying to create a stopwatch in a browser where the time is either captured upon 'capture' (allowing the timer to continue) or 'stop' (which will also stop the timer).我正在尝试在浏览器中创建一个秒表,其中时间是在“捕获”(允许计时器继续)或“停止”(这也将停止计时器)时捕获的。 To do so, I want a single html button to alternate between two labels and corresponding functions ('start' and 'stop and capture').
为此,我需要一个 html 按钮来在两个标签和相应功能(“开始”和“停止和捕获”)之间切换。 I have tried to use two instances of addEventListener, each with an associated removeEventListener.
我尝试使用 addEventListener 的两个实例,每个实例都有一个关联的 removeEventListener。 After reading this useful article , I wrote the following code:
在阅读了这篇有用的文章后,我编写了以下代码:
var button = document.getElementById("start");
function onStart() {
start();
document.getElementById("capture").style.visibility = 'visible'; // show capture button
document.getElementById("start").innerHTML = "Stop & Capture";
button.addEventListener("click", function() {
onCapture(); // assigns value to a new variable
onStop();
button.removeEventListener("click", arguments.callee, false); // remove this EventListener
}, false);
}
function onStop() {
stop();
document.getElementById("capture").style.visibility = 'hidden'; // hide capture button
document.getElementById("start").innerHTML = "Start";
button.addEventListener("click", function() {
onStart();
button.removeEventListener("click", arguments.callee, false); // remove this EventListener
}, false);
}
However, the behaviour of the 'start' / 'stop and capture' button is unexpected and saves more and more time instances every time it is pressed.但是,“开始”/“停止并捕获”按钮的行为是出乎意料的,每次按下它都会节省越来越多的时间实例。 My guess was that the removeEventListener isn't working, but I might be wrong.
我的猜测是 removeEventListener 不起作用,但我可能错了。 The associsated html is:
相关的 html 是:
<div>Time: <span id="time"></span></div>
<button onclick="onStart()" id="start" class="start" style="width:150px">Start</button>
<button onclick="onCapture()" id="capture" style="visibility:hidden">Capture</button>
<button onclick="reset()" id="reset">Reset</button>
So, after spending far too long trying various solutions, it turned out that the problem was using removeEventListener
with anonymous functions.因此,在花费了太长时间尝试各种解决方案之后,结果发现问题在于使用带有匿名函数的
removeEventListener
。 Here is the code that worked properly:这是正常工作的代码:
function onStart() {
document.getElementById("start").removeEventListener("click", onStart, false); // remove listener
start();
document.getElementById("capture").style.visibility = 'visible'; // show capture button
document.getElementById("start").innerHTML = "Stop & Capture";
document.getElementById("start").addEventListener("click", onStop, false); // add listener
}
function onStop() {
document.getElementById("start").removeEventListener("click", onStop, false); // remove listener
onCapture();
stop();
document.getElementById("capture").style.visibility = 'hidden'; // hide capture button
document.getElementById("start").innerHTML = "Start";
document.getElementById("start").addEventListener("click", onStart, false); // add listener
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.