您的位置:首页 > Web前端 > JavaScript

javascript-倒计时-(咋个办呢-zgbn)

2016-02-03 10:29 543 查看

javascript-倒计时

下面的代码复制后直接可用

<html>
<script type="text/javascript">
/**
* @parame eid 元素ID,将倒计时空间绑定在元素上
* @parame startDateTime 倒计时开始日期时间 countTime = {YY:年,MM:月,DD:日,HH:时,MS:分,SS:秒}
* @parame unit 单位,按那个单位倒计时。例如'SS'按秒,'MS'按分;可选参数,默认为'SS'
* @parame format 倒计时显示的样式:YYYY-MM-DD HH:MS:SS 或者 MS分:SS秒 可选参数,默认为 HH:MS:SS
* return boolean
*/
function counDownTime(eid,startDateTime,unit,format) {

if(!window._unit && !window._format && !window._eid && !window._count && !window._sumSS && !window.CDate){
if(eid && startDateTime){
window._unit = unit||'SS' ;
window._format = format||'HH:MS:SS' ;
window._eid = eid ;
window._count = window._count||0 ;
window._sumSS = 'SS'==window._unit?1000:'MS'==window._unit?60*1000:'HH'==window._unit?60*60*1000:'DD'==window._unit?24*60*60*1000:1000 ;

window.CDate = new Date(startDateTime.YY,startDateTime.MM-1,startDateTime.DD,startDateTime.HH,startDateTime.MS,startDateTime.SS) ;
}
else{
return false ;
}
}

window._count = window._count+window._sumSS-0 ;

var nowDate = new Date() ;

var setEshow = function(date){

var stime = window._format ;

if(stime.indexOf('YYYY')!=-1){
stime = stime.replace('YYYY', date?date.getFullYear():0) ;
}
if(stime.indexOf('MM')!=-1){
stime = stime.replace('MM', date?date.getMonth()+1:0)
}
if(stime.indexOf('DD')!=-1){
stime = stime.replace('DD', date?date.getDate():0)
}
if(stime.indexOf('HH')!=-1){
stime = stime.replace('HH', date?date.getHours():0)
}
if(stime.indexOf('MS')!=-1){
stime = stime.replace('MS', date?date.getMinutes():0)
}
if(stime.indexOf('SS')!=-1){
stime = stime.replace('SS', date?date.getSeconds():0)
}

return stime ;
} ;

var destructor = function(){
window._unit = null ;
window._format = null ;
window._eid = null ;
window._count = null ;
window._sumSS = null ;

window.CDate = null ;
}

if(nowDate.getTime() >= window.CDate.getTime()){
document.getElementById(window._eid).innerHTML = setEshow(null);
destructor() ;
return true ;
}
else{
document.getElementById(window._eid).innerHTML = setEshow(new Date(window.CDate.getTime()-window._count));
}

setTimeout("counDownTime()",window._sumSS) ;
}

</script>
<body>

<h1>倒计时</h1>

<span id='time' style='font-size:40px;color:#F00'>05.00</span><span id='time' style='font-size:40px;color:#F00'>s</span>

<input type=button value='倒计时' onclick='counDownTime("time",{YY:2011,MM:7,DD:28,HH:15,MS:0,SS:0},null)' />

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: