倒计时插件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MPFLY/article/details/81709255

传入一个时间字符串,格式为:HH:mm:ss

function timer(time){
    var newTime = ""
    var h = parseInt(time.split(':')[0])
    var m = parseInt(time.split(':')[1])
    var s = parseInt(time.split(':')[2])
    var timers = setInterval(function(){
        if(s == 0){
            s = 60;
            m--;
            s--;

            if(m == 0){
                if(h == 0){

                }else{
                    m = 59;
                    h--;
                    s--;
                }
            }
        }else{
            s--;
        }
        if(s == 0 && m==0 && h==0){
            clearInterval(timers)
        }
        newTime = addZero(h)+":"+addZero(m)+":"+addZero(s)
        console.log(newTime)
    },1000)

}

下面的方法为搭配使用的方法:该方法的作用是:假如“时”、“分”、“秒”中的数字是一位数时,在它之前补零。

function addZero(num){
    var num_ = num+'';

    if(num_.length != 2){
        return '0'+num
    }else{
        return num
    }
}

猜你喜欢

转载自blog.csdn.net/MPFLY/article/details/81709255
今日推荐