You should not use a timeout or setinterval when you want to rely on real expiring time as them are not reliable. Also, notice that a refresh cancel the setTimeout/setInterval.
Instead you should use a setInterval that check every x seconds if the session has expired checking it against current Date() object.
Example:
setInterval(function(){
var decodedToken = localStorage.getItem('user_token');
if (decodedToken.exp < new Date().getTime()/1000) {
// Token is expired
}
}, 3000);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…