简体   繁体   English

如何在 setInterval 中更改 Variable 的值

[英]How to change the value of Variable in setInterval

the Value of time will change just one time in the method setInterval i would like to change it with the value of checkLevel时间值在 setInterval 方法中只会改变一次我想用 checkLevel 的值来改变它

var time = checkLevel(); var time = checkLevel();

//die Methode wird nach msecLevel aufgerufen //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);

You can create a recursive function that changes the interval, then simply call the interval once to start it, then the function will just change the interval every call.您可以创建一个更改间隔的递归函数,然后只需调用一次间隔即可启动它,然后该函数将在每次调用时更改间隔。

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

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

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