實(shí)現(xiàn)倒計(jì)時(shí)效果需要掌握js中的兩個(gè)知識(shí)點(diǎn):
	1、setTimeout函數(shù) 每隔1秒鐘更新秒鐘時(shí)間
	2、Date對(duì)象 計(jì)算時(shí)間差
下面貼出 元旦倒計(jì)時(shí)代碼
<!DOCTYPE html><html> <head> <title>example.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script> //定義計(jì)時(shí)器,每隔1秒鐘調(diào)用timer函數(shù) var timerCount= window.setTimeout("timer()",1000); function timer(){ var date=new Date("2017","1","1","0","0","0"); var deadlineTime=date.getTime(); var nowDate=new Date(); var nowTime=nowDate.getTime(); var distTime=deadlineTime-nowTime; //判斷是否到達(dá)時(shí)間期限 if(distTime>0){ var d=Math.floor(distTime/1000/3600/24); var h=Math.floor(distTime/1000/3600%24); var m=Math.floor(distTime/1000/60%60); var s=Math.floor(distTime/1000%60); var str="距離2017年元旦還有"+d+"天"+h+"時(shí)"+m+"分"+s+"秒"; document.getElementById("timeout").innerHTML=str; timerCount= window.setTimeout("timer()",1000); } else{ //到達(dá)時(shí)間期限的時(shí)候清楚計(jì)時(shí)器; window.clearTimeout(timerCount); alert("時(shí)間已到"); } } </script> </head> <body> <p id="timeout"> </p></html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
 
  | 
新聞熱點(diǎn)
疑難解答