函数内的间隔被调用数百次

Interval within function gets called hundreds of times

当我调用一次函数时,间隔是可以的,它是按预期添加和清除的。当再次调用同一个函数时,间隔将被调用数百次。 我怎样才能停止这种行为,我的功能有什么问题?

function draw(){
    ctx.clearRect(0,0,width,height);
    ctx.fillStyle = 'blue';

    // Add cube width 5px
    if(cube.width <= 300){
        cube.width += 5;
    }

    // Draw it on the screen
    ctx.fillRect(cube.x,cube.y,cube.width,cube.height);

    // Create function b(e) to check cubes collision
    function b(e){
        var x = e.clientX - ctx.canvas.offsetLeft;
        var y = e.clientY - ctx.canvas.offsetTop;

        if(x >= cube.x && x <= cube.x + cube.width
            && y >= cube.y && y <= cube.y + cube.height){
            ctx.canvas.removeEventListener('click',b,false);
            level1();
            return;
        }
    }

    ctx.canvas.addEventListener('click', b, false);

    // Set the interval to 60 FPS
    var timer = setInterval(function(){
        check += 1;

        console.log(check);

        if(check > 50){
            return;
        }
        // Call again the same function
        draw();
    },1000/60);
}

我怀疑这是一个 javascript 问题,所以我的猜测是 canvas 与此无关。

更新 我从中调用的函数(在另一个函数中):

    function a(e){
        var x = e.clientX - ctx.canvas.offsetLeft;
        var y = e.clientY - ctx.canvas.offsetTop;

        if(x >= player.x && x <= player.x + player.width
            && y >= player.y && y <= player.y + player.height){
            ctx.canvas.removeEventListener('click',a,false);
            draw();
        }
    }

    // Add the click event using the previous function
    ctx.canvas.addEventListener('click', a, false);

您应该只调用 setInterval() 一次并且在绘制函数之外。这样做的方式是启动越来越多的计时器,导致 draw() 函数被调用的次数无限增加。 或者:"To execute a function only once, after a specified number of milliseconds, use the setTimeout() method"。参考:Window setIntervalsetTimeout() 可以在你的 draw() 函数中使用。

据我所知,我发布了这个答案

function draw(check){
    ctx.clearRect(0,0,width,height);
    ctx.fillStyle = 'blue';

    // Add cube width 5px
    if(cube.width <= 300){
        cube.width += 5;
    }

    // Draw it on the screen
    ctx.fillRect(cube.x,cube.y,cube.width,cube.height);

    // Create function b(e) to check cubes collision
    function b(e){
        var x = e.clientX - ctx.canvas.offsetLeft;
        var y = e.clientY - ctx.canvas.offsetTop;

        if(x >= cube.x && x <= cube.x + cube.width
            && y >= cube.y && y <= cube.y + cube.height){
            ctx.canvas.removeEventListener('click',b,false);
            level1();
            return;
        }
    }

    ctx.canvas.addEventListener('click', b, false);

    check+ = 1;
    if(check <=50)
    {
       // Set the interval to 60 FPS
       var timer = setTimeout(function()
       {
         // Call again the same function
         draw(check);
       },1000/60);
    }
}

更新我从中调用 draw 的函数(在另一个函数中):

function a(e){
    var x = e.clientX - ctx.canvas.offsetLeft;
    var y = e.clientY - ctx.canvas.offsetTop;

    if(x >= player.x && x <= player.x + player.width
        && y >= player.y && y <= player.y + player.height){
        ctx.canvas.removeEventListener('click',a,false);
        var check = 0;
        draw(check);
    }
}

// Add the click event using the previous function
ctx.canvas.addEventListener('click', a, false);

您可以在外部某处声明检查变量并将其设置为 0,然后再从其他函数调用绘图函数,或者您可以将带有初始值的检查变量传递给绘图函数