简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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