Continual counter regardless of page refresh

后端 未结 5 1758
不知归路
不知归路 2021-01-13 17:14

I have this piece of jQuery that currently increments a number by one every 5 seconds. The problem I have is that its client side, therefore it resets every time you refresh

5条回答
  •  迷失自我
    2021-01-13 17:37

    Check this DEMO

    //Counter
    var counter=22000000000;
    if(typeof(localStorage.getItem('counts'))!='object')
    {
       counter=parseInt(localStorage.getItem('counts'));
    }
    setInterval(function () {
        $(".count").html(counter);
        ++counter;
        localStorage.setItem('counts',counter);
    }, 1000);
    

    Highlight on localStorage

    localStorage is an implementation of the Storage Interface. It stores data with no expiration date, and gets cleared only through JavaScript, or clearing the Browser Cache / Locally Stored Data - unlike cookie expiry.

提交回复
热议问题