求一个高考倒计时的js代码,加解释

JavaScript018

求一个高考倒计时的js代码,加解释,第1张

var scount = 0

$(function () {

var start = setInterval(function () {

var dtstr = ""

var now = new Date()//初始化当前时间

var newdatr = new Date(now.getFullYear(), "05", "07", "00", "00", "00", "00")//初始化目标时间(这里的05实际上是六月)

var nowhms = now.getTime()//当前时间转换毫秒

var newhms = newdatr.getTime()//目标时间转换毫秒

var cha = newhms - nowhms//时间差(毫秒数)

if (cha >0) {

var day = parseInt(cha / (1000 * 60 * 60 * 24))//计算天数

var hh = parseInt((cha % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))//计算小时

var min = parseInt(((cha % (1000 * 60 * 60 * 24)) % (1000 * 60 * 60)) / (1000 * 60))//计算分钟

var ss = parseInt((((cha % (1000 * 60 * 60 * 24)) % (1000 * 60 * 60)) % (1000 * 60)) / 1000)//计算秒

if (ss == 0) {

scount += 1

}

//var ms = parseInt((((cha % (1000 * 60 * 60 * 24)) % (1000 * 60 * 60)) % (1000 * 60)) % 1000)

dtstr = day + "天" + hh + "小时" + min + "分钟" + ss + "秒"//+ ms + "毫秒"

}

$("#timestr").html(dtstr)

$("#flag").html("你已经续了" + scount + "分钟。")

}, 1000)

})

这是2014年高考倒计时的代码,可以自己修改只需要更改BirthDay=new Date("7-6-2014")里面的数字就可以。测试可用。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<title>无标题文档</title>

</head>

<body onload="show_date_time()">

<SCRIPT language=javascript>

function show_date_time(){

window.setTimeout("show_date_time()", 1000)

BirthDay=new Date("7-6-2014")

today=new Date()

timeold=(BirthDay.getTime()-today.getTime())

sectimeold=timeold/1000

secondsold=Math.floor(sectimeold)

msPerDay=24*60*60*1000

e_daysold=timeold/msPerDay

daysold=Math.floor(e_daysold)

e_hrsold=(e_daysold-daysold)*24

hrsold=Math.floor(e_hrsold)

e_minsold=(e_hrsold-hrsold)*60

minsold=Math.floor((e_hrsold-hrsold)*60)

seconds=Math.floor((e_minsold-minsold)*60)

span_dt_dt.innerHTML="<b><align=center><font color=00CC00>"+daysold+"天"+hrsold+"小时"+minsold+"分"+seconds+"秒"+"</b><br></font>"

}

</SCRIPT>

<b><align=center><font color=#ffffff>高考倒计时还有:</b></font>

<span id="span_dt_dt" style='align=center no:1px solid blackbackground-color:#FFFFFF' ></span>

</body>

</html>