简体   繁体   English

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

[英]Interval within function gets called hundreds of times

When I call the function once, the interval is ok, it is added and cleared as supposed to. 当我一次调用该函数时,间隔就可以了,可以按照预期的方式添加和清除该间隔。 When the same function gets called again, the interval is called hundreds of times. 当再次调用同一函数时,间隔被调用数百次。 How can I stop this behavior and what is wrong with my function? 如何停止这种行为,我的功能出了什么问题?

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);
}

I suspect this is a javascript problem, so my guess is that canvas has nothing to do with this. 我怀疑这是一个JavaScript问题,所以我的猜测是画布与此无关。

Update The function I am calling draw from (which is inside another function): 更新我正在调用的函数来自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);
            draw();
        }
    }

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

What i understood, according to that i am posting this answer 根据我的理解,我正在发布此答案

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);
    }
}

Update The function I am calling draw from (which is inside another function): 更新我正在调用的函数来自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);

You can declare the check variable somewhere outside and set it to 0 before calling the draw function from some other function or you can just pass the check variable with initial value what ever you want to the draw function 您可以在其他函数调用draw函数之前,在外部的某个地方声明check变量并将其设置为0,或者可以将具有所需初始值的check变量传递给draw函数

You should call setInterval() only once and outside the draw function. 您只能在draw函数外部调用一次setInterval() The way you do it, you are starting an increasing number of timers that cause the draw() function to be called an infinitely growing number of times. 这样做的方式是,启动了越来越多的计时器,这些计时器导致draw()函数被调用的次数无限增加。 Alternatively: "To execute a function only once, after a specified number of milliseconds, use the setTimeout() method". 或者:“要在指定的毫秒数后仅执行一次功能,请使用setTimeout()方法”。 Reference: Window setInterval . 参考: 窗口setInterval setTimeout() could be used inside your draw() function. setTimeout()可以在draw()函数中使用。

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

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