我有一段 jQuery,目前每 5 秒将一个数字递增 1。我遇到的问题是它的客户端,因此每次刷新页面时它都会重置。
相反,我希望计数器继续运行,即使您不在网站上也不管您刷新页面多少次,这就是为什么我认为服务器端脚本(如 PHP)更适合我的使用案件。如果没有,请提出其他建议。
我已经设置了我目前使用 jQuery 的工作 fiddle :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
is an implementation of the Storage Interface. It stores data withno expiration date
, and gets cleared only throughJavaScript
, or clearing theBrowser Cache
/Locally Stored Data
- unlike cookie expiry.
关于javascript - 无论页面刷新如何,持续计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30572762/