js實(shí)現(xiàn)倒計(jì)時(shí)器自定義時(shí)間和暫停
js倒計(jì)時(shí)器可自定義時(shí)間和暫停,效果如下,點(diǎn)擊start 開(kāi)始計(jì)時(shí),end結(jié)束計(jì)時(shí)
分別復(fù)制 H5 css js 代碼即可實(shí)現(xiàn),具體的算法在js控制函數(shù)中(都寫(xiě)了注釋)
css
html,body{ width:100%;height:100%; } .content{ height:100%;width:100%; } .row-center{ display:flex;flex-direction:row;justify-content:center; align-items:center; } .tc-input-style{ outline:none;border:none;width:20%;height:80%;border-radius:10px; } .tc-span-style{ width:30%;height:100%;font-weight:bold; } .tc-font-style{ font-size:15px;font-weight:bold; } .tc-div-style1{ height:33%;width:100% } .tc-div-style0{ height:30%;width:100% } .tc-div-style2{ height:10%;width:100%; } .tc-div-style3{ height:100%;width:30% } .column-center{ display:flex;flex-direction:column;justify-content:center; align-items:center; } .column-start-center{ display:flex;flex-direction:column;justify-content:flex-start; align-items:center; } #timecount{ height:50%;width:20%; } .button-style-0{ border-radius:10px; } .row-space-around{ display:flex;flex-direction:row;justify-content:space-around; align-items:center; }
h5
<body> <div class="content row-center"> <div id="timecount" class="column-center tc-font-style" > <div class="column-start-center tc-div-style0" > <div class="row-center tc-div-style1" ><span class="tc-span-style row-center">小時(shí):</span><input class=tc-input-style id="hour_count" value="0"> </div> <div class="row-center tc-div-style1" ><span class="tc-span-style row-center">分鐘:</span><input class=tc-input-style id="minute_count" value="0"> </div> <div class="row-center tc-div-style1" ><span class="tc-span-style row-center">秒:</span><input class=tc-input-style id="second_count" value="0"> </div> </div> <div class="row-center tc-div-style2"> <div class="row-center tc-div-style3" id="hour_show" ></div> <div class="row-center tc-div-style3" id="minute_show"></div> <div class="row-center tc-div-style3" id="second_show"></div> </div> <div class="row-space-around tc-div-style2"> <button class="button-style-0" onclick="timecounts()">start</button> <button class="button-style-0" onclick="timestop()">stop</button> </div> </div> </div> </body>
記得引入jq
<script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
JS
<script type="text/javascript"> var timecount;//定義一個(gè)全局變量 function timer(intDiff){ //定義一個(gè)循環(huán)函數(shù)每一秒定時(shí)執(zhí)行 timecount=setInterval(function(){ var hour=0, minute=0, second=0;//初始化時(shí)間默認(rèn)值 //算法控制 if(intDiff > 0){ hour = Math.floor(intDiff / (60 * 60)) ; minute = Math.floor(intDiff / 60) - (hour * 60); second = Math.floor(intDiff) - (hour * 60 * 60) - (minute * 60); } if (minute <= 9) minute = '0' + minute; if (second <= 9) second = '0' + second; //打印到dom $('#hour_show').html('<s id="h"></s>'+hour+'時(shí)'); $('#minute_show').html('<s></s>'+minute+'分'); $('#second_show').html('<s></s>'+second+'秒'); intDiff--; }, 1000); console.log(intDiff); } function timecounts(){ console.log($("#hour_count").val()) count=parseInt($("#hour_count").val()*3600)+parseInt($("#minute_count").val()*60)+parseInt($("#second_count").val()) timer(count);//調(diào)用計(jì)時(shí)器函數(shù) console.log(count); } function timestop(){ var hour= $("#hour_show").text(); var minute= $("#minute_show").text(); var second= $("#second_show").text(); var time=parseInt($("#hour_show").text())*3600+parseInt($("#minute_show").text())*60+parseInt($("#second_show").text()) console.log(time); timecount=window.clearInterval(timecount);//停止計(jì)時(shí)器 } </script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 用JS寫(xiě)了一個(gè)30分鐘倒計(jì)時(shí)器的實(shí)現(xiàn)示例
- JS基于面向?qū)ο髮?shí)現(xiàn)的多個(gè)倒計(jì)時(shí)器功能示例
- JavaScript分秒倒計(jì)時(shí)器實(shí)現(xiàn)方法
- VB倒計(jì)時(shí)器和JS當(dāng)前時(shí)間
- JavaScript定時(shí)器設(shè)置、使用與倒計(jì)時(shí)案例詳解
- js定時(shí)器實(shí)現(xiàn)倒計(jì)時(shí)效果
- js 倒計(jì)時(shí)(高效率服務(wù)器時(shí)間同步)
- js實(shí)現(xiàn)瀏覽器倒計(jì)時(shí)跳轉(zhuǎn)頁(yè)面效果
- javascript同步服務(wù)器時(shí)間和同步倒計(jì)時(shí)小技巧
- JS實(shí)現(xiàn)簡(jiǎn)單網(wǎng)頁(yè)倒計(jì)時(shí)器
相關(guān)文章
Flex通過(guò)JS獲取客戶端IP和計(jì)算機(jī)名的實(shí)例代碼
這篇文章主要介紹了Flex通過(guò)JS獲取客戶端IP和計(jì)算機(jī)名的實(shí)例代碼,有需要的朋友可以參考一下2013-11-11JavaScript Perfection kill 測(cè)試及答案
近日,在Perfection kill上看到有關(guān)javascript quiz。并做了一下,最終錯(cuò)了2個(gè)(#2,#9),但是,這2道題,在Ie和ff下的答案是不一樣的?!2010-03-03文本框中禁止非數(shù)字字符輸入比如手機(jī)號(hào)碼、郵編
總是遇到很多禁止非數(shù)字字符輸入的文本框,比如手機(jī)號(hào)碼了 郵編了于是下面為大家介紹下如何禁止,感興趣的朋友可以了解下2013-08-08webpack實(shí)現(xiàn)一個(gè)行內(nèi)樣式px轉(zhuǎn)vw的loader示例
這篇文章主要介紹了webpack實(shí)現(xiàn)一個(gè)行內(nèi)樣式px轉(zhuǎn)vw的loader示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09說(shuō)明你的Javascript技術(shù)很爛的五個(gè)原因
Javascript在互聯(lián)網(wǎng)上名聲很臭,但你又很難再找到一個(gè)像它這樣如此動(dòng)態(tài)、如此被廣泛使用、如此根植于我們的生活中的另外一種語(yǔ)言。2011-04-04利用prop-types第三方庫(kù)對(duì)組件的props中的變量進(jìn)行類型檢測(cè)
本篇文章主要介紹了利用prop-types第三方庫(kù)對(duì)組件的props中的變量進(jìn)行類型檢測(cè)的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-05-05