简体   繁体   English

javascript:dblclick不会清除间隔

[英]javascript: dblclick will not clear interval

  // sets loop to zero var totalTenths = 0; var interval; var startButton = document.querySelector('#start'); // start and pause button document.querySelector('#start').addEventListener('click', function (e) { var startButton = e.target; if (startButton.innerHTML === 'Start') { startButton.innerHTML = 'Pause'; interval = setInterval(countTimer, 10) colorInterval = setInterval(colorTimer, 1000) } else if (e.target.innerHTML === 'Pause') { startButton.innerHTML = 'Resume'; clearInterval(interval); clearInterval(colorInterval); // here I'm setting the 15 second restart interval waitedTooLong = setInterval(timeout, 15000) } else if (startButton.innerHTML === 'Resume') { startButton.innerHTML = 'Pause'; interval = setInterval(countTimer, 10) colorInterval = setInterval(colorTimer, 1000) } }); // double click to clear function document.querySelector('#start').addEventListener('dblclick', function(e) { var startButton = e.target; if (startButton.innerHTML === 'Resume') { clearInterval(function() { setInterval(countTimer, 10) }); document.getElementById('tenths').innerHTML = '00'; document.getElementById('seconds').innerHTML = '00'; document.getElementById('minutes').innerHTML = '00'; document.getElementById('start').innerHTML = 'Start' } }); // loop that converts 10th of millisec to minute and seconds function countTimer() { totalTenths++; var minutes = Math.floor(totalTenths / 6000); var seconds = Math.floor((totalTenths - minutes * 6000) / 100); var tenths = totalTenths - (minutes * 6000 + seconds * 100); // replaces inner html with loop with added zero until double digits accure if (tenths > 0) { document.getElementById('tenths').innerHTML = '0' + tenths; } if (tenths > 9) { document.getElementById('tenths').innerHTML = tenths; } if (tenths > 9) { document.getElementById('seconds').innerHTML = '0' + seconds; } if (seconds > 9) { document.getElementById('seconds').innerHTML = seconds; } if (tenths > 0) { document.getElementById('minutes').innerHTML = '0' + minutes; } if (minutes > 9) { document.getElementById('minutes').innerHTML = minutes; } } 
  <div class="text-center container"> <button id="start" class="btn btn-large btn-success">Start</button> <p class="clear-msg">double click to clear!</p> <div id="timer" class="well"> <span id="minutes">00</span>:<span id="seconds">00</span>:<span id="tenths">00</span> </div> </div> 

I have a setInterval called Interval which runs a countup timer. 我有一个名为Interval的setInterval,它运行一个计数计时器。 I have a start button that plays on first initial click and pauses on second just fine. 我有一个开始按钮,可以在第一次单击时播放,而在第二次单击时暂停就可以了。 When I double click, it displays the timer back to zero, but doesn't seem to be clearing the actual timer. 当我双击时,它会将计时器显示为零,但似乎并未清除实际的计时器。 Will just play where it was left off before the display was replaced with zeros. 仅在将显示替换为零之前播放掉的地方。

I do not have the privilege to comment, so this goes here. 我没有发表评论的权限,因此请转到此处。 It's not a complete answer, hate working via apps. 这不是一个完整的答案,讨厌通过应用程序工作。

From what I have searched, and know, is that on click will fire before double click. 从我搜索过的,知道的是,单击将在双击之前触发。 You can try a different even like on long press or something. 您甚至可以像长按一样尝试其他操作。 Or you can try this method, 或者您可以尝试这种方法,

How to use both onclick and ondblclick on an element? 如何在元素上同时使用onclick和ondblclick?

What you have to do is handle the timeout between clicks and decide if it qualifies as a double or a single. 您要做的是处理两次点击之间的超时,并确定其是否为两次或一次。 I think that should do it. 我认为应该这样做。

I'll try and post code when I can reach my comp. 当我能达到我的要求时,我将尝试发布代码。 Have a great day. 祝你有美好的一天。

Your main issue was your global totalTenths variable, which was not being reset after a double-click. 您的主要问题是全局totalTenths变量,双击后未重置该变量。 You also weren't clearing the colorInterval timer after a timeout or a double-click. 您也没有在超时或双击后清除colorInterval计时器。 Properly killing both timers, resetting the variable and re-initializing the display solves the issue. 正确取消这两个计时器,重置变量并重新初始化显示即可解决此问题。

Next, organizing your declarations can be of great help here. 接下来,在这里整理声明非常有用。 Instead of scanning the document for the same elements over and over, cache references to them in variables. 不必一遍又一遍地在文档中扫描相同的元素,而是将对它们的引用缓存在变量中。

Also your colorTimer function was unnecessary as all it does is wrap the changeColor function. 另外,您的colorTimer函数是不必要的,因为它所做的只是包装changeColor函数。

Lastly, it's crucial with multiple timers to always stop any currently running timers before starting another one that invokes the same function again. 最后,对于多个计时器而言,至关重要的是始终停止任何当前正在运行的计时器,然后再启动另一个再次调用相同功能的计时器。 You needed to clear both your timers in the final branch of your "click" event handler's if statement. 您需要在“ click”事件处理程序的if语句的最后一个分支中清除两个计时器。

You had a lot of duplication of code that makes things more difficult to read and debug. 您有很多重复的代码,使事情变得更难阅读和调试。 Follow the DRY (Don't Repeat Yourself) principle when coding. 编码时,请遵循DRY(请勿重复自己)原则。 If you find yourself typing the same code twice, you are probably doing something wrong. 如果发现自己两次键入相同的代码,则可能是您做错了什么。 Here is a much more compact and cleaned up working version with comments inline to explain: 这是一个更加紧凑和清理的工作版本,带有内联注释以解释:

 // sets loop to zero // This is the varible that essentially hold the elapsed time. It's global so it must be // reset upon a timeout or a double-click var totalTenths = 0; // Always initialize your variables to something, use null if you don't // have an actual value yet. var interval = null; var colorInterval = null; var waitedTooLong = null; // Get DOM references just once and then use them throughout the rest of the code var startButton = document.getElementById('start'); var m = document.getElementById('minutes'); var s = document.getElementById('seconds'); var t = document.getElementById('tenths'); // start and pause button startButton.addEventListener('click', function (e) { // No need to test what object you're dealing with // you are here because the start button got clicked // that makes "this" === startButton // Don't use .innerHTML when you are only working with raw text // use textContent instead. if (this.textContent === 'Start') { startButton.textContent = 'Pause'; interval = setInterval(countTimer, 10) colorInterval = setInterval(changeColor, 1000) } else if (this.textContent === 'Pause') { startButton.textContent = 'Resume'; clearInterval(interval); clearInterval(colorInterval); // here I'm setting the 15 second restart interval waitedTooLong = setInterval(timeout, 15000) } else { // Clear prior timers clearInterval(interval); clearInterval(colorInterval); this.textContent = 'Pause'; interval = setInterval(countTimer, 10) colorInterval = setInterval(changeColor, 1000) } }); // double click to clear function which is the same as the timeout function startButton.addEventListener('dblclick', timeout); // changes second's color function changeColor() { var red = Math.round(Math.random() * 255); var green = Math.round(Math.random() * 255); var blue = Math.round(Math.random() * 255); s.style.color = 'rgb(' + red + ', ' + green + ', ' + blue + ')'; } // loop that converts 10th of millisec to minute and seconds function countTimer() { totalTenths++; var minutes = Math.floor(totalTenths / 6000); var seconds = Math.floor((totalTenths - minutes * 6000) / 100); var tenths = totalTenths - (minutes * 6000 + seconds * 100); // replaces inner html with loop with added zero until double digits accure if (tenths > 0) { t.textContent = '0' + tenths; } if (tenths > 9) { t.textContent = tenths; } if (tenths > 9) { s.textContent = '0' + seconds; } if (seconds > 9) { s.textContent = seconds; } if (tenths > 0) { m.textContent = '0' + minutes; } if (minutes > 9) { m.textContent = minutes; } } // 15 second restart funciton function timeout() { clearInterval(interval); clearInterval(colorInterval); t.textContent = '00'; s.textContent = '00'; m.textContent = '00'; startButton.textContent = 'Start' s.style.color = "#000"; // You must reset this global variable for the counter to reset properly totalTenths = 0; // <<------------------------ } 
 <script type="text/javascript" src="https://cdn.polyfill.io/v2/polyfill.min.js?features=es6,fetch,Array.prototype.includes"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <div class="text-center container"> <button id="start" class="btn btn-large btn-success">Start</button> <p class="clear-msg">double click to clear!</p> <div id="timer" class="well"> <span id="minutes">00</span>: <span id="seconds">00</span>: <span id="tenths">00</span> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM