var
interval
=
1000
function
ShowCountDown(year,month,day,divname)
{
var
now
=
new
Date()
var
endDate
=
new
Date(year,
month-1,
day)
var
leftTime=endDate.getTime()-now.getTime()
var
leftsecond
=
parseInt(leftTime/1000)
//var
day1=parseInt(leftsecond/(24*60*60*6))
var
day1=Math.floor(leftsecond/(60*60*24))
var
hour=Math.floor((leftsecond-day1*24*60*60)/3600)
var
minute=Math.floor((leftsecond-day1*24*60*60-hour*3600)/60)
var
second=Math.floor(leftsecond-day1*24*60*60-hour*3600-minute*60)
var
cc
=
document.getElementById(divname)
cc.innerHTML
=
"脚本之家提示距离"+year+"年"+month+"月"+day+"日还有:"+day1+"天"+hour+"小时"+minute+"分"+second+"秒"
}
window.setInterval(function(){ShowCountDown(2010,4,20,'divdown1')},
interval)
[Ctrl+A
全选
注:如需引入外部Js需刷新才能执行]
利用new Date()可以轻松的实现钟表功能,甚至日历功能.如果要实现计时器功能也可以用这个对象.
var c = 1000 // 一千微秒,就是一秒
function funBeginDisTime() {
c = c + 1000 // 节奏为一秒
var now = new Date(0,0,0,0,0,0,c)
var day = now.getDate()
var hour = now.getHours()
var minutes = now.getMinutes()
var sec = now.getSeconds()
$("#myClock").html(day + "天"+ hour + "时" + minutes + "分" + sec + "秒")
myTime = setTimeout("funBeginDisTime()", 1000)// 每一秒执行一次
}
function funStopDisTime() {
clearTimeout(myTime)
}
body>
<input id="Button2" type="button" value="开始" onclick="funBeginDisTime()"/>
<span id="myClock"></span>
<input id="Button1" type="button" value="暂停" onclick="funStopDisTime()" />
</body>
setInterval() 是循环重复执行某个动作,
setTimeout()是只执行一次.
比如每五秒就通过AJAX向服务器发送一次请求.那么就可以用setInterval():
[javascript] view plain copy
setInterval("reloadAction()", 5000)
[javascript] view plain copy
function reloadAction() {
$.ajax({
"type":"POST",
"url":"live.php",
"data":"getData=live",
"success":function(data) {
// ....
}
})
}