倒计时封装

1.传入一个结束时间    eg:'2018-12-01 00:00:00'

var timer = null;

function time(that, time, cd) {

    console.log("当前时间", time)

    clearInterval(timer);

    if (time == '') {

        return false

    }

    var endTime = new Date(time).getTime() || 0,

        nowTime = new Date().getTime(),

        hours = 60 * 60,

        minutes = 60,

        lfTime = (endTime - nowTime) / 1000;

    if (lfTime < 0) {

        return cd()

    }

    timer = setInterval(function() {

        lfTime--;

        var Hours = Math.floor(lfTime / hours),

            Minutes = Math.floor((lfTime - Hours * hours) / minutes),

            Seconds = Math.floor((lfTime - Hours * hours - Minutes * minutes));

        Hours = Hours < 10 ? "0" + Hours : Hours;

        Minutes = Minutes < 10 ? "0" + Minutes : Minutes;

        Seconds = Seconds < 10 ? "0" + Seconds : Seconds;

        var time = {

            hour: Hours,

            min: Minutes,

            second: Seconds

        }

        if (Hours === '00' && Minutes === "00" && Seconds === "00") {

            clearInterval(timer);

            cd()

        }

        return that.setData({

            time: time

        })

    }, 1000)

}

module.exports = time;



2.调用方法  import  timer from '/你存放的路径'

3.使用   

timer(this, '2018-12-01 00:00:00', () => {

            console.log(1235567)

        })

你可能感兴趣的:(倒计时封装)