页面刷新时不会重新开始的(javascript)倒计时?
我想知道是否有任何解决方案可以实现 JavaScript 计数器,该计数器不会在页面刷新时重置,但会继续计数,直到设定的时间过去。
你对此了解多少?
谢谢
i was wondering if there is any solution for implementing a javascript counter that doesn't reset at page refreshm but it continues counting until the set time passes.
What do you know about it?
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将当前倒数计时器计数存储在 cookie 中(每次计时器更改时您都会这样做)。然后,当页面加载(或刷新)时,首先检查该 cookie 的值,如果已设置,则使用该值作为倒数计数器的起点。
You could store the current countdown timer count in a cookie (you would do that every time the timer changes). And then when the page loads (or refreshes) check for the value of that cookie first, and if it is set use that value as the starting point of the countdown counter.
您需要将其状态存储在某个地方,无论是在 cookie 中还是在服务器端。
在每次页面加载时,代码都需要检查其状态并相应地调整其倒计时。
You will need to store its state somewhere, either in a cookie or serverside.
On each page load, the code will need to check its state and adjust its countdown accordingly.
您可以使用 localStorage 或 cookie 来存储您当前的计数器值。
这是一个示例: http://jsfiddle.net/ArtBIT/KsYE4/
You can use localStorage or cookies to store your current counter value.
Here's an example: http://jsfiddle.net/ArtBIT/KsYE4/