利用jQuery+localStorage實現(xiàn)一個簡易的計時器示例代碼
前言
在HTML5中,新加入了一個localStorage特性,這個特性主要是用來作為本地存儲來使用的,解決了cookie存儲空間不足的問題(cookie中每條cookie的存儲空間為4k),localStorage中一般瀏覽器支持的是5M大小,這個在不同的瀏覽器中l(wèi)ocalStorage會有所不同。
本文主要介紹了關(guān)于jQuery+localStorage實現(xiàn)簡易計時器的相關(guān)內(nèi)容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。
原型
需求
1.關(guān)閉瀏覽器時時間繼續(xù)運行
2.刷新時保持當前狀態(tài)
3.結(jié)束時間保存在客戶端
示例代碼
<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.循環(huán)的間隔。 var stopwatchClock = $(".container.stopwatch").find(".clock"), stopwatchDigits = stopwatchClock.find('span'); // 檢查前一個會話是否在秒表運行時結(jié)束。 // 如果是的話,按時間重新開始。 //即 關(guān)閉瀏覽器,點擊開始,在后臺保持計時的狀態(tài) if(Number(localStorage.stopwatchBeginingTimestamp) && Number(localStorage.stopwatchRunningTime)){ var runningTime = Number(localStorage.stopwatchRunningTime) + new Date().getTime() - Number(localStorage.stopwatchBeginingTimestamp); localStorage.stopwatchRunningTime = runningTime; startStopwatch(); } //如果前一個會話有運行時間,就把它寫在時鐘上。 // 如果沒有初始化為0。 //即結(jié)束時不可刷新 if(localStorage.stopwatchRunningTime){ stopwatchDigits.text(returnFormattedToMilliseconds(Number(localStorage.stopwatchRunningTime))); } else{ localStorage.stopwatchRunningTime = 0; } /* 實現(xiàn)開始結(jié)束 */ $("#stopwatch-btn-start").toggle(function() { $(this).text ('開始').css("background", "#3bb4f2"); if(stopwatchClock.hasClass('inactive')){ startStopwatch() } }, function() { $(this).text ('結(jié)束').css("background", "red"); pauseStopwatch(); }) // Pressing the clock will pause/unpause the stopwatch. //按下暫停/恢復(fù)的時鐘秒表 /*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; // 應(yīng)用程序還記得上一次會話運行了多長時間。 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)){ // 計算運行時間。 // 新的運行時間=上次運行時間+現(xiàn)在-最后一次啟動 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>
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- 使用jquery讀取html5 localstorage的值的方法
- jQuery訪問瀏覽器本地存儲cookie、localStorage和sessionStorage的基本用法
- jQuery timers計時器簡單應(yīng)用說明
- 基于JQuery.timer插件實現(xiàn)一個計時器
- jquery 顯示*天*時*分*秒實現(xiàn)時間計時器
- 基于jquery插件編寫countdown計時器
- jQuery實現(xiàn)簡單的計時器功能實例分析
- sliderToggle在寫jquery的計時器setTimeouter中不生效
- jQuery實現(xiàn)倒計時功能 jQuery實現(xiàn)計時器功能
- jquery實現(xiàn)一個全局計時器(商城可用)
相關(guān)文章
JQuery fileupload插件實現(xiàn)文件上傳功能
這篇文章主要介紹了JQuery fileupload插件實現(xiàn)文件上傳功能的相關(guān)資料,需要的朋友可以參考下2016-03-03JQuery實現(xiàn)動態(tài)表格點擊按鈕表格增加一行
動態(tài)表格,功能為點擊添加按鈕,表格增加一行并給其name屬性賦予的值,點擊刪除,自動刪除這一行,具體實現(xiàn)如下2014-08-08jquery 給動態(tài)生成的標簽綁定事件的幾種方法總結(jié)
下面小編就為大家分享一篇jquery 給動態(tài)生成的標簽綁定事件的幾種方法總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02jQuery實現(xiàn)在最后一個元素之前插入新元素的方法
這篇文章主要介紹了jQuery實現(xiàn)在最后一個元素之前插入新元素的方法,涉及jquery針對頁面元素的匹配選擇與屬性操作相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07jQuery實現(xiàn)動態(tài)添加和刪除input框代碼實例
這篇文章主要介紹了jQuery實現(xiàn)動態(tài)添加和刪除input框,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03JQuery中綁定事件(bind())和移除事件(unbind())
本文主要向大家詳細介紹了jQuery的綁定事件和移除事件的使用方法和示例分享,這里推薦給有需要的小伙伴們參考下。2015-02-02jquery檢測input checked 控件是否被選中的方法
這篇文章主要介紹了jquery檢測input checked 控件是否被選中的方法,需要的朋友可以參考下2014-03-03