[SOLVED] How to detect device or computer "going to sleep"

Hello!

My game has endless arcade style rounds where at the end there is an API call to my server to submit started_at and ended_at times to calculate how long users lasted. im getting insane scores on my server every once in a while. Usually round times are lik 1-6 min, but occasionally i see 500 min+. This is really impossible in my game and my presumption is that the device went to sleep or something.

My current solution i thought should work but has proven not to. Does anyone see issues with my solution or have other solutions that would work better than mine?

Any advice would be helpful!

Solution Summary:
Reset a new date() timestamp every time the user clicks, touches, or does a keyboard key on this browser tab. If the last input was over a min old, refresh the browser immediately, also before we submit the data on gameover, we check if the input was over a min old, if so we don’t submit the game

GameDataController.js:

GameDataController.prototype.initialize = function() {
    // Setup listeners for any user interaction
    pc.BSM.Input.lastInputDateTime = new Date();
    this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.resetLastInput, this);
    this.app.keyboard.on(pc.EVENT_KEYDOWN, this.resetLastInput, this);
    try {
        this.app.touch.on(pc.EVENT_TOUCHSTART, this.resetLastInput, this);
    } catch (e){
        console.log("No touch listener - must not be mobile");
    }
};

//Reset our pc.BSM.Input.lastInputDateTime whenever user input is detected
//If it has been over 60 secodns since last input, just restart the browser
GameDataController.prototype.resetLastInput = function(){
    var currentDate = new Date();
    var seconds = Math.abs(pc.BSM.Input.lastInputDateTime - currentDate) / 1000;
    if (seconds > 60 && pc.SceneController.currentSceneIsGame()){
        //Reload if its been over a min during a game since the last input
        location.reload();
    } else {
        pc.BSM.Input.lastInputDateTime = new Date();
    }
};

//This is called when the player dies and the game ends
GameDataController.prototype.submitGameScore = function() {
    //If its been more than 60 seconds since user input, dont submit game score
    var currentDate = new Date();
    var seconds = Math.abs(pc.BSM.Input.lastInputDateTime - currentDate) / 1000;
    console.log("Seconds since last input = "+seconds.toString());
    if (seconds > 60){
        //Send error to raygun so we can track if this happens a lot
        location.reload();
        return;
    }

    ///API Call Submit Game Code Down Here
};

I’m curious,

Why are you calculating the time based on dates? You could count the seconds with the update function byt adding dt to a number prototype. Since the update function doesn’t actually run while the window isn’t actively rendering, this would solve the problem of the counter continuing to rise when the computer is asleep, or if the user changed tabs and the application was no longer visible.

Thanks for the idea, that sounds like a great alternative! I believe my issue is solved and my strategy worked - i realized from looking at the data, the data that seems to have surpassed this check came from an old version - so probably their browser cached the old code before this was implemented. Seems to be all good now!