倒計時主要用到的知識點:1、設置時間間隔的setInterval可以被clearInterval取消 2、毫秒轉換為時分格式 這個是效果圖 下麵是js中的函數 第二個是html資源,為了方便我css直接寫在html中了 需要代碼的小伙伴可以自行下載 鏈接:http://pan.baidu.com/s ...
倒計時主要用到的知識點:1、設置時間間隔的setInterval可以被clearInterval取消
2、毫秒轉換為時分格式
這個是效果圖
下麵是js中的函數
var shijian=3600; var time=null; function start(){ time=setInterval("count()",1000);//返回值time只是為了需要暫停的時候clearInterval(time)清除時間間隔 } function count(){ if(shijian<0){ alert("time out"); pause();//一般情況下這個if條件是用來提交數據用的,這裡只是測試一下。 }else{ $("#time p").html(Math.floor(shijian/60)+":"+shijian%60);//這裡用到將毫秒轉換到時分格式 shijian--; console.info(time); } } function pause(){ clearInterval(time); } function goOn(){ time = setInterval("count()",1000);//重新設置時間間隔 }
第二個是html資源,為了方便我css直接寫在html中了
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="1.js"></script> <script src="jquery-1.8.3.min.js"></script> <style type="text/css"> input{ background-color: #9fc5f1; width: 100px; height: 30px; line-height: 30px; text-align: center; color: #fff; font-size:14px; font-weight: bold; } #time p{ color: #1f6dc2; font-size: 48px; font-weight: bold; margin-left:90px; margin-bottom:0px; } </style> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <body> <div id="time"> <p>00:00</p> <input type="button" value="開始" onclick="start()"/> <input type="button" value="暫停" onclick="pause()"/> <input type="button" value="繼續" onclick="goOn()"/> </div> </body> </html>
需要代碼的小伙伴可以自行下載
鏈接:http://pan.baidu.com/s/1boZ8Xun 密碼:ymu0