简体   繁体   English

如何停止JavaScript中的setInterval()计时器失控

[英]How do I stop a runaway setInterval() timer in JavaScript

setInterval hangs when I hold the left mouse button down and sequentially press the right button. 当我按住鼠标左键并依次按右键时,setInterval会挂起。 setInterval will not clear after that. 之后,setInterval将不会清除。

    var timer = 0;

    document.getElementById('c_3').onmousedown = function(e) {

          timer = setInterval(function() {myFunction();}, 30);

          if (e.which == 2 || e.which == 3) {

                clearInterval(timer);

          }

    }

This is how it works now ... 现在是这样的...

    let timer = 0;

    document.getElementById('c_3').onmousedown = function(e) {

      if (e.which == 1) {

        timer = setInterval(function() {myFunction();}, 30);

      }

      if (e.which == 2 || e.which == 3) {

        document.getElementById('c_3').oncontextmenu = function() {

          return false;

        }

        clearInterval(timer);

      }

    }

    document.getElementById('c_3').onmouseup = function() {

        clearInterval(timer);

    }

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

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