无论页面刷新如何,连续计数器 [英] Continual counter regardless of page refresh

查看:128
本文介绍了无论页面刷新如何,连续计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段jQuery,它每5秒就会增加一个数字。我的问题是它的客户端,因此它会在每次刷新页面时重置。



相反,即使您离开,我也希望柜台继续运行该网站以及您刷新页面的次数,这就是为什么我认为像PHP这样的服务器端脚本更适合我的用例。如果不是,请另行说明。

我已经设置了一个我现在使用jQuery的小工具: http://jsfiddle.net/f354bzy5/



什么是PHP重新创建这种影响,包括我上面的要求?



以下是我正在使用的jquery:

  // Counter 
var counter = 22000000000;
$(。count)。html(counter);
setInterval(function(){
$(。count)。html(counter);
++ counter;
},5000);


解决方案

检查 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);

突出显示 localStorage


localStorage 是存储界面的一个实现。它存储
数据,其中 没有过期日期 ,并且只能通过
JavaScript 或清除 浏览器缓存 / 本地存储数据 -
不像cookie过期。



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);

解决方案

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.

这篇关于无论页面刷新如何,连续计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆