简体   繁体   English

sessionStorage注册

[英]sessionStorage registration

I got few problems on a script 我在脚本上遇到了一些问题

I can't understand why my timer still keep on going while "minutes" and "secondes" === 0 Second thing, if i'm reloading the page my timer left. 我不明白为什么我的计时器为什么在“分钟”和“秒” === 0时仍然继续运行,如果我正在重新加载计时器剩下的页面,那我就不知道了。

let get_sec = sessionStorage.getItem('sec_storage');
let get_min = sessionStorage.getItem('min_storage');

if (!get_sec && !get_min){
    //Sessionstorage vide
    secondes = 10;
    minutes = 0;
}
let interval = setInterval(function () {
    secondes--;
    sessionStorage.setItem('sec_storage', get_sec);
    sessionStorage.setItem('min_storage', get_min);
    $('#count-timer').text('Expiration in : ' + minutes + ' minutes ' + secondes + ' secondes ');
    if (secondes === 0) {
        minutes--;
        secondes = 59;
    }
    if(minutes <= 0 && secondes <= 0){
        clearInterval(interval);
        sessionStorage.removeItem('sec_storage');
        sessionStorage.removeItem('min_storage');
        $('#count-timer').text('Sorry, reservation expired');
    }
}, 1000);

In these blocks 在这些街区

if (secondes === 0) {
    minutes--;
    secondes = 59;
}
if(minutes <= 0 && secondes <= 0){
    clearInterval(interval);
    sessionStorage.removeItem('sec_storage');
    sessionStorage.removeItem('min_storage');
    $('#count-timer').text('Sorry, reservation expired');
}

If the secondes value is === 0, you change it to 59. therefore, in your last if , secondes is never lower nor equal to 0 and clearInterval(interval); 如果secondes值=== 0,则将其更改为59。因此,在最后一个ifsecondes永远不会低于也不等于0且clearInterval(interval); is never called 从未被称为

Add this verification in your first if block 在您的第一个if区块中添加此验证

if ((minutes > 0) && (secondes === 0)) {
    minutes--;
    secondes = 59;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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