doulieyu0881 2015-06-01 11:50
浏览 172
已采纳

无论页面刷新如何,都会持续计数

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 the page.

Instead I'd like the counter to continue even if you are away from the site and regardless of how many times you refresh the page, which is why I thought a server side script such as PHP would be better suited to my use case. If not please suggest otherwise.

I've setup a working fiddle of what I currently have with jQuery: http://jsfiddle.net/f354bzy5/

What would be the PHP to recreate this affect that include my requirements above?

Here's the Jquery I'm using:

//Counter
var counter = 22000000000;
$(".count").html(counter);
  setInterval(function () {
  $(".count").html(counter);
  ++counter;
}, 5000);
  • 写回答

5条回答 默认 最新

  • douliang2087 2015-06-01 12:12
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题