Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript设置/清除间隔辅助_Javascript_Jquery_Html_Html5 Canvas - Fatal编程技术网

Javascript设置/清除间隔辅助

Javascript设置/清除间隔辅助,javascript,jquery,html,html5-canvas,Javascript,Jquery,Html,Html5 Canvas,对于决定回答这个问题的人来说,我突然遇到了一个小问题。 我试图做到以下几点: 创造 var intrvl; //for interval function strt() { //and set interval intrvl = setInterval(writeT,3000); } function writeT() { //do something here as the interval runs checkIf(); //check for the


对于决定回答这个问题的人来说,我突然遇到了一个小问题。
我试图做到以下几点:

创造

var intrvl; //for interval
function strt() {
     //and set interval
     intrvl = setInterval(writeT,3000);
}
function writeT() {
     //do something here as the interval runs
     checkIf(); //check for the mouse down
}
function checkIf() {
     //here goes the code that checks if mouse is down and if it is
     //then I call another function that begins other useful process
     //I tried
     if (c.addEventListener('click')) { //c is my canvas
          clearInterval(intrvl); //guess clearing the interval yee?
          //and then calling the function I want to call
          startDoing();
     }
}
我想等待并使用该时间间隔,直到有人单击画布,然后运行所需的功能。
但是每当我在画布上单击时,函数
startdoong()
碰巧正在运行,但与不运行所有这些函数相比,它太快了。

我怎样才能让它工作?因为我希望第一个创建的interval不存在,只有
startdoong()
运行。

您误解了addEventListener()的用法

checkIf()
中的条件立即返回。因此,您的代码是

  • 调用
    strt()时启动间隔计时器
  • 在间隔计时器第一次过期时添加单击处理程序
  • 但它也会立即停止间隔时间er并调用
    startdoong()
    then
  • 这样使用:

    var intrvl;
    var clicked = false;
    
    function strt() {
         //and set interval
         intrvl = setInterval(checkIf,3000);
    }
    
    function checkIf() {
        if ( clicked ) {
            clearInterval( intrvl );
            startDoing();
        }
    }
    
    c.addEventListener('click', function() {
        clicked = true;
    } );
    
    此代码立即注册click处理程序。调用strt()后,它将立即启动间隔计时器。单击鼠标后,单击的变量将设置为true以标记此事件。下次您的间隔计时器触发时,它会识别前面的单击,因为它会检查该变量并启动您想要启动的任何程序