前言
在HTML5中,新加入了一個(gè)localStorage特性,這個(gè)特性主要是用來(lái)作為本地存儲(chǔ)來(lái)使用的,解決了cookie存儲(chǔ)空間不足的問(wèn)題(cookie中每條cookie的存儲(chǔ)空間為4k),localStorage中一般瀏覽器支持的是5M大小,這個(gè)在不同的瀏覽器中l(wèi)ocalStorage會(huì)有所不同。
本文主要介紹了關(guān)于jQuery+localStorage實(shí)現(xiàn)簡(jiǎn)易計(jì)時(shí)器的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話(huà)不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
原型
需求
1.關(guān)閉瀏覽器時(shí)時(shí)間繼續(xù)運(yùn)行
2.刷新時(shí)保持當(dāng)前狀態(tài)
3.結(jié)束時(shí)間保存在客戶(hù)端
示例代碼
<p class="wrapper"> <p class="app"> <p class="container stopwatch"> <p class="clock inactive z-depth-1"> <span>0:00:00</span> <!-- <p class="overlay waves-effect"></p>--> </p> <form> <a id="stopwatch-btn-start" class="waves-effect waves-teal btn-flat">開(kāi)始</a> </form> </p> </p> </p>
<script> // Stopwatch var stopwatchInterval = 0; // The interval for our loop.循環(huán)的間隔。 var stopwatchClock = $(".container.stopwatch").find(".clock"), stopwatchDigits = stopwatchClock.find('span'); // 檢查前一個(gè)會(huì)話(huà)是否在秒表運(yùn)行時(shí)結(jié)束。 // 如果是的話(huà),按時(shí)間重新開(kāi)始。 //即 關(guān)閉瀏覽器,點(diǎn)擊開(kāi)始,在后臺(tái)保持計(jì)時(shí)的狀態(tài) if(Number(localStorage.stopwatchBeginingTimestamp) && Number(localStorage.stopwatchRunningTime)){ var runningTime = Number(localStorage.stopwatchRunningTime) + new Date().getTime() - Number(localStorage.stopwatchBeginingTimestamp); localStorage.stopwatchRunningTime = runningTime; startStopwatch(); } //如果前一個(gè)會(huì)話(huà)有運(yùn)行時(shí)間,就把它寫(xiě)在時(shí)鐘上。 // 如果沒(méi)有初始化為0。 //即結(jié)束時(shí)不可刷新 if(localStorage.stopwatchRunningTime){ stopwatchDigits.text(returnFormattedToMilliseconds(Number(localStorage.stopwatchRunningTime))); } else{ localStorage.stopwatchRunningTime = 0; } /* 實(shí)現(xiàn)開(kāi)始結(jié)束 */ $("#stopwatch-btn-start").toggle(function() { $(this).text ('開(kāi)始').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ù)的時(shí)鐘秒表 /*stopwatchClock.on('click',function(){ if(stopwatchClock.hasClass('inactive')){ startStopwatch() } else{ pauseStopwatch(); } });*/ /*開(kāi)始計(jì)時(shí)*/ function startStopwatch(){ // 防止多個(gè)間隔同時(shí)進(jìn)行。 clearInterval(stopwatchInterval); var startTimestamp = new Date().getTime(), runningTime = 0; localStorage.stopwatchBeginingTimestamp = startTimestamp; // 應(yīng)用程序還記得上一次會(huì)話(huà)運(yùn)行了多長(zhǎng)時(shí)間。 if(Number(localStorage.stopwatchRunningTime)){ runningTime = Number(localStorage.stopwatchRunningTime); } else{ localStorage.stopwatchRunningTime = 1; } // 每隔100ms重新計(jì)算運(yùn)行時(shí)間,計(jì)算公式是 // 當(dāng)你上次啟動(dòng)時(shí)鐘+上次運(yùn)行時(shí)間 stopwatchInterval = setInterval(function () { var stopwatchTime = (new Date().getTime() - startTimestamp + runningTime); stopwatchDigits.text(returnFormattedToMilliseconds(stopwatchTime)); }, 100); stopwatchClock.removeClass('inactive'); } /*停止計(jì)時(shí)*/ function pauseStopwatch(){ // 停止計(jì)時(shí) clearInterval(stopwatchInterval); if(Number(localStorage.stopwatchBeginingTimestamp)){ // 計(jì)算運(yùn)行時(shí)間。 // 新的運(yùn)行時(shí)間=上次運(yùn)行時(shí)間+現(xiàn)在-最后一次啟動(dòng) 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>
相信看了本文案例你已經(jīng)掌握了方法,更多精彩請(qǐng)關(guān)注Gxl網(wǎng)其它相關(guān)文章!
推薦閱讀:
jQuery向動(dòng)態(tài)列表添加新元素
實(shí)用的jQuery抽屜式在線(xiàn)客服
怎么使用jQuery選中并且操作table
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com