原型

图片发自简书App

需求
1.关闭浏览器时时间继续运行
2.刷新时保持当前状态
3.结束时间保存在客户端


<div class="wrapper">
<div class="app">
<div class="container stopwatch"> <div class="clock inactive z-depth-1">
<span>0:00:00</span>
<!-- <div class="overlay waves-effect"></div>-->
</div> <form>
<a id="stopwatch-btn-start" class="waves-effect waves-teal btn-flat">开始</a> </form> </div>
</div>
</div>
<script>

    // Stopwatch
var stopwatchInterval = 0; // The interval for our loop.循环的间隔。 var stopwatchClock = $(".container.stopwatch").find(".clock"),
stopwatchDigits = stopwatchClock.find('span'); // 检查前一个会话是否在秒表运行时结束。
// 如果是的话,按时间重新开始。
//即 关闭浏览器,点击开始,在后台保持计时的状态
if(Number(localStorage.stopwatchBeginingTimestamp) && Number(localStorage.stopwatchRunningTime)){ var runningTime = Number(localStorage.stopwatchRunningTime) + new Date().getTime() - Number(localStorage.stopwatchBeginingTimestamp); localStorage.stopwatchRunningTime = runningTime; startStopwatch();
} //如果前一个会话有运行时间,就把它写在时钟上。
// 如果没有初始化为0。
//即结束时不可刷新
if(localStorage.stopwatchRunningTime){ stopwatchDigits.text(returnFormattedToMilliseconds(Number(localStorage.stopwatchRunningTime))); }
else{
localStorage.stopwatchRunningTime = 0;
} /* 实现开始结束 */
$("#stopwatch-btn-start").toggle(function() {
$(this).text ('开始').css("background", "#3bb4f2");
if(stopwatchClock.hasClass('inactive')){
startStopwatch()
} }, function() { $(this).text ('结束').css("background", "red");
pauseStopwatch();
}) // Pressing the clock will pause/unpause the stopwatch.
//按下暂停/恢复的时钟秒表 /*stopwatchClock.on('click',function(){ if(stopwatchClock.hasClass('inactive')){
startStopwatch()
}
else{
pauseStopwatch();
}
});*/ /*开始计时*/
function startStopwatch(){
// 防止多个间隔同时进行。
clearInterval(stopwatchInterval); var startTimestamp = new Date().getTime(),
runningTime = 0; localStorage.stopwatchBeginingTimestamp = startTimestamp; // 应用程序还记得上一次会话运行了多长时间。
if(Number(localStorage.stopwatchRunningTime)){
runningTime = Number(localStorage.stopwatchRunningTime);
}
else{
localStorage.stopwatchRunningTime = 1;
} // 每隔100ms重新计算运行时间,计算公式是
// 当你上次启动时钟+上次运行时间 stopwatchInterval = setInterval(function () {
var stopwatchTime = (new Date().getTime() - startTimestamp + runningTime); stopwatchDigits.text(returnFormattedToMilliseconds(stopwatchTime));
}, 100); stopwatchClock.removeClass('inactive');
} /*停止计时*/
function pauseStopwatch(){
// 停止计时
clearInterval(stopwatchInterval); if(Number(localStorage.stopwatchBeginingTimestamp)){ // 计算运行时间。
// 新的运行时间=上次运行时间+现在-最后一次启动
var runningTime = Number(localStorage.stopwatchRunningTime) + new Date().getTime() - Number(localStorage.stopwatchBeginingTimestamp); localStorage.stopwatchBeginingTimestamp = 0;
localStorage.stopwatchRunningTime = runningTime; stopwatchClock.addClass('inactive');
}
} // 重置.
/*function resetStopwatch(){
clearInterval(stopwatchInterval); stopwatchDigits.text(returnFormattedToMilliseconds(0));
localStorage.stopwatchBeginingTimestamp = 0;
localStorage.stopwatchRunningTime = 0; stopwatchClock.addClass('inactive');
}
*/ function returnFormattedToMilliseconds(time){
var
seconds = Math.floor((time/1000) % 60),
minutes = Math.floor((time/(1000*60)) % 60),
hours = Math.floor((time/(1000*60*60)) % 24);
seconds = seconds < 10 ? '0' + seconds : seconds;
minutes = minutes < 10 ? '0' + minutes : minutes;
return hours + ":" + minutes + ":" + seconds;
}
</script>

完整的demo下载链接:https://pan.baidu.com/s/1kVOmt4V 密码:rfqq

原文作者:祈澈姑娘
原文链接:https://www.jianshu.com/u/05f416aefbe1
创作不易,转载请告知

90后前端妹子,爱编程,爱运营,爱折腾。
坚持总结工作中遇到的技术问题,坚持记录工作中所所思所见,欢迎大家一起探讨交流。
有个很难涨粉的的公众号叫:【编程微刊】

最新文章

  1. centos7下操作防火墙
  2. java utils
  3. JAVA抓取URL
  4. HttpClient post json,可以是数组
  5. Spring表单参数绑定中对“is”开头的boolean类型字段的的处理
  6. 【Android测试】【第十二节】Uiautomator——API详解
  7. [pdf.js]预览pdf时,中文名称乱码的问题
  8. 如何给magento的产品页面添加返回按钮
  9. 使用jaxp对比xml进行DOM解析
  10. 简单jQuery实现选项框中列表项的选择
  11. php字符串截取问题
  12. 【mysql的设计与优化专题(1)】ER图,数据建模与数据字典
  13. bzoj3637: Query on a tree VI
  14. XP Mode 虛擬機器 for Windows 7
  15. 201521123101 《Java程序设计》第5周学习总结
  16. KNN-笔记(1)
  17. Netty权威指南(笔记一)
  18. 网站被植入Webshell的解决方案
  19. java线上应用问题排查方法和工具
  20. 关于电脑宽带显示连接 qq可以登录 但是无法上网的问题

热门文章

  1. JdbcTemplate简单介绍
  2. ASP.NET MVC案例教程(基于ASP.NET MVC beta)——第二篇:第一个页面
  3. 软件——机器学习与Python,Python3的输出与输入
  4. jQuery快速入门知识重点
  5. 基于深度学习的目标检测(object detection)—— rcnn、fast-rcnn、faster-rcnn
  6. 软件——keil的查找,错误,不能跳转到相应的行
  7. 【 2017 Multi-University Training Contest - Team 9 && hdu 6162】Ch’s gift
  8. 洛谷——P1089 津津的储蓄计划
  9. Log Explorer 使用说明(原创)
  10. 总览:SpringCloud基础结构