我正在使用我在谷歌找到的倒计时器,我只是修改一些代码行,使计时器计数而不是倒计时。我只想让我的计时器计数,然后当我点击停止按钮时它停止。我的问题是,即使页面刷新或浏览器重新启动,我也希望计时器不重置。据我所知,我需要为此使用 cookie,但我不知道如何将其合并到我的代码中。请帮忙..
这是我的代码。
(函数 () {
$ (文档).ready (函数 () {
Var 时间 = “00:00:00”,
零件 = 时间。拆分 (':' ',
小时 = 部分 [0],
分钟数 = 部分 [1],
秒 = 部分 [2],
Span = $ ('# countup');
函数校正 (编号) {
返回 (num <10)?(“0” num): num;
}
Var 定时器 = setInterval (函数 () {
秒;
如果 (秒> 59) {
分钟;
秒 = 0;
如果 (分钟> 59) {
小时;
秒 = 0;
分钟 = 0;
如果 (小时> = 24) {
警报 (“计时器完成”);
}
}
}
Span.text (correctNum (小时) ”:“ correctNum (分钟) ”:“ correctNum (秒));
},1000);
});
} )()
00:00:00
I think u should use localstorage for store time. its better to store time on unload page event
The best way to persist a countdown is probably to save the end of the countdown as a fixed point in time (such as a UNIX timestamp, which JavaScript has support for) and then re-calculate the difference between local time and the saved time when the page loads. There are actually such handly libraries that help out with time operations, such as Moment.js.
我建议在 cookie 上使用 LocalStorage,cookie 也会被发送到服务器,并且更多的是为身份验证/会话令牌而设计的。这里有一篇很好的文章可以开始: