How to solve the problem that the setInterval () timer stops execution after jumping to the page?

when I do the registration page and send the CAPTCHA countdown, I save the second seconds of the countdown as cookie, so that I can continue to execute the timer when I refresh and leave the page, instead of starting from the heart.
but when I use window.location.href to jump to the page and go back to the registration page, I find that the number of seconds is still the number of seconds I left the page

has anyone solved it?

//
$(".btnStr").click(function(){
    var second = 30;
    start(second);
})
function start(second){
    var timer = setInterval(function(){
        if(second>0){
            second--;
            $(".btnStr").html(second+"");
        }else{
            $(".btnStr").html("");
            clearInterval(timer);
        }
        setCookie("second",second,1);
    },1000)
}
//cookie
function setCookie(cname,cvalue,exdays){
    var d = new Date();
    d.setTime(d.getTime()+(exdays*24*60*60*1000));
    var expires = "expires="+d.toGMTString();
    document.cookie = cname+"="+cvalue+"; "+expires;
}
//cookie
function getCookie(cname){
    var name = cname + "=";
    var ca = document.cookie.split(";");
    for(var i=0; i<ca.length; iPP) {
        var c = ca[i].trim();
        if (c.indexOf(name)==0) { return c.substring(name.length,c.length); }
    }
    return "";
}
//cookie
function checkCookie(){
    var overplus=getCookie("second");
    if (overplus!=0){
        start(overplus);
    }
}
Jun.29,2021

if you leave the page, you won't have to maintain the countdown in cookie. When you cut the page back, it must have started counting from the point when you left.
you can use the system time. 2 are stored in cookie, one is the countdown, and the other is the time to start the countdown. When you cut back, you will make a judgment with the current time to correct the offset value.


the number of seconds should not be saved is not the start time of sending a CAPTCHA to determine whether the difference between the current time and the current time is 60 seconds


without looking at the code; When the page destroys the js timer must have been destroyed, you can click send CAPTCHA to record the new Date after 30 seconds is several seconds, record this time in cookie, when you return to the page, to see if it is time to cookie or use cookie time-time to return to the page


your page left, of course stopped execution ah.
recommendations are as follows:
1. The backend needs to record the delivery time of CAPTCHA
2. Don't worry about it on that digital page. Refresh and start over. After clicking get, if the backend still has 30s to judge, return the 30s, and you can put it on the button

.
Menu