Google Chrome restores session cookies after a crash, how to avoid?

前端 未结 2 627
南方客
南方客 2021-01-18 13:30

On Google Chrome (I saw this with version 35 on Windows 8.1, so far I didn\'t try other versions) when browser crashes (or you simply unplug power cable...)

2条回答
  •  北海茫月
    2021-01-18 14:11

    Adriano's suggestion makes is a good idea but the implementation is flawed. We need to remember the time from before the crash so we can compare it to the time after the crash. The easiest way to do that is to use sessionStorage.

    const CRASH_DETECT_THRESHOLD_IN_MILLISECONDS = 10000;
    
    const marker = parseInt(sessionStorage.getItem('crashDetectMarker') || new Date().valueOf());
    
    const diff = new Date().valueOf() - marker;
    console.log('diff', diff)
    
    if (diff > CRASH_DETECT_THRESHOLD_IN_MILLISECONDS) {
        alert('log out');
    } else {
        alert ('ok');
    }
    
    setInterval(() => {
        sessionStorage.setItem('crashDetectMarker', new Date().valueOf());
    }, 1000)
    
    

    To test, you can simulate a Chrome crash by entering chrome://crash in the location bar.

    Don't forget to clear out the crashDetectMarker when the user logs out.

提交回复
热议问题