简体   繁体   English

在 JavaScript 的单个按钮上使用 addEventListener 和 removeEventListener 两次

[英]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.

相关问题 Javascript:addEventListener(),removeEventListener()和bind() - Javascript: addEventListener(), removeEventListener() and bind() addEventListener 和 removeEventListener 使用 forEach - addEventListener and removeEventListener using forEach 带有部分功能的Javascript addEventListener和removeEventListener - Javascript addEventListener and removeEventListener with partial function 使用addEventListener javascript添加后,removeEventListener()不会删除函数 - removeEventListener() not removing function after added with addEventListener javascript 尝试使用使用 JavaScript 的 addEventListener 重用按钮 - Trying to reuse a button using addEventListener using JavaScript 如何在JavaScript中的方法中的addEventListener和全局函数中的removeEventListener时调用removeEventLisener - How to call removeEventLisener when addEventListener in a method and removeEventListener in a global function in JavaScript Javascript- removeEventListener 未应用于数组,从之前添加的 addEventListener 使用 for 循环 - Javascript- removeEventListener not applied to array, from previous addEventListener added with for loop 如何使用 JavaScript addEventListener() 方法禁用按钮? - how to disable button using JavaScript addEventListener() method? 在使用addEventListener添加事件之前,请使用removeEventListener删除事件不起作用 - remove an event using removeEventListener before adding an event using addEventListener is not working Javascript addEventListener到动态按钮 - Javascript addEventListener to the dynamic button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM