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

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值