带 cookie 的 JavaScript 倒计时器
我有一个倒计时器,它会显示筹款的目标金额,例如 1000000 美元,并在几天内慢慢倒数到零。我得到了这个片段:
$(function()
{
var cnt = 75000000;
var count = setInterval(function()
{
if (cnt > 0)
{
$('#target').html("<span>KSHS</span><strong>" + cnt + " Target </strong>");
cnt--;
}
else
{
clearInterval(count);
$('#target').html("<strong> Target Achieved! </strong>");
}
}, 4000);
});
唯一的问题是,每次刷新页面时,计数器都会再次启动,这本质上意味着它永远不会结束。
我希望当用户重新访问/刷新页面时,计数器持续存在并继续。我读过 javascript cookies 可以用于此目的,只是不知道如何实现它们,有什么帮助吗?
I have a countdown timer that will show a target amount to be fundraised like USD1000000 and slowly count backwards to zero over a period of days. I got this snippet:
$(function()
{
var cnt = 75000000;
var count = setInterval(function()
{
if (cnt > 0)
{
$('#target').html("<span>KSHS</span><strong>" + cnt + " Target </strong>");
cnt--;
}
else
{
clearInterval(count);
$('#target').html("<strong> Target Achieved! </strong>");
}
}, 4000);
});
The only problem is that everytime you refresh the page the counter starts again which essentially means it will never end.
I'd like it that a when a user revisits/refreshes the page the counter persists and continues. I've read that javascript cookies can be used for this, just don't know how to implement them, any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这个答案:
javascript-cookie-timeout-with-countdown-timer< /a>
Take a look at this SO answer:
javascript-cookie-timeout-with-countdown-timer