如何改变setInterval中Variable的值

How to change the value of Variable in setInterval

时间的值在方法setInterval中只会改变一次 我想用 checkLevel

的值来改变它

var time = checkLevel();

//die Methode wird nach msecLevel aufgerufen

    setInterval(function (event) {
       
        time = checkLevel();
        if (!isLoser) {
            //Leere die Fläche
            ctx.clearRect(0, 0, canvasWidth, canvasHeight);
            //Neu durchführen
            randomX = getRandomPosition(canvasWidth - fishSize);
            randomY = getRandomPosition(canvasHeight - fishSize);
            var randomFish = Math.floor(Math.random() * 11) + 1;
            fish.src = 'images/fish' + randomFish + '.png';

        }
    }, time);

您可以创建一个递归函数来更改间隔,然后只需调用一次间隔来启动它,然后该函数将在每次调用时更改间隔。

var _timer;

    function showFish(){
           if (!isLoser) {
                //Leere die Fläche
                ctx.clearRect(0, 0, canvasWidth, canvasHeight);
                //Neu durchführen
                randomX = getRandomPosition(canvasWidth - fishSize);
                randomY = getRandomPosition(canvasHeight - fishSize);
                var randomFish = Math.floor(Math.random() * 11) + 1;
                fish.src = 'images/fish' + randomFish + '.png';
           }

         _timer = setTimeout(showFish,  checkLevel());
    }
    
     _timer = setTimeout(showFish,  checkLevel());