Is it possible to create 2 cycles in javascript, where one cycle will be a priority in case of a lack of resources? (Both handle game tics)

The problem is this:

In the multi-player game js and asm.js, I have two loops.

One handles the actual game tics, such as position position, speed and battle.

Another handles rendering of this world on canvas so that the user can see.

What I would like to do is when the processor / GPU (they did the same on some machines now, I can’t say that I am happy with this) is too burdened, the rendering cycle should skip and therefore stop changing canvas. That is, freezing the game screen in the lags of the lag.

At the same time, a small computing power is used to successfully complete the actual tick of the game, preventing desynchronization with other game clients.

(This is an RTS game when it comes to loading, so user input is sent over the network instead of the positions of all objects).

Otherwise, the client will have to kick other clients, or all clients will have to suspend its reconnection and re-synchronization. that is bad bad bad!

, , , . -, , , " " , . , , , , , , - , .

, ( ) , , , ( 10 ).

, , , ?

: , / , (.. 100 , , / ).

+4
2

- , javascript . -, .

, ui.

, - ​​ -

+1

Fo , setInterval, - requestAnimationFrame. - requestAnimationFrame , , .


,

- , requestAnimationFrame , - javascript. setTimeout. , . , setTimeout. requestAnimationFrame. - - ( ). , . , .

: , , , , . "". , , - . . , , ? . . "", 2 . - , - .

javascript. , . :

var seconds = 0;
setInterval(function(){ seconds++; var x = 10e8; while(--x); }, 1000);

, 10 "" 10.

: , Date.now() :

var setLogicalLoop = (function(){

    var _startedAt,
        _stop,
        _ms;


    function frame(){
        if (_stop === true) 
            return;

        // calculations

        var dt = Date.now() - _startedAt,
            diff = dt % _ms;

        setTimeout(frame, ms - diff);
    };

    return function (callback, ms){

        _startedAt = Date.now();
        _stop = false;

        setTimeout(frame, ms);


        return function(){
            _stop = true;
        };
    };
});


// -> start
var stopLoop = setLogicalLoop(myFunction, ms);
// -> stop
stopLoop();
0

All Articles