实现一天的倒计时

 function Countdown() {
    var _this=this;
            this.timeObject = setInterval(function () {
                            //获取当前时间
                            var date = new Date();
                            var now = date.getTime();
                            //设置截止时间
                            var endDate = (new Date(date.getFullYear(),date.getMonth(),date.getDate(),23,59,59))
                            var end = endDate.getTime();
                            //时间差
                            var leftTime = end - now;
                            //定义变量 h,m,s保存倒计时的时间
                            var d = 0, h = 0, m = 0, s = 0;
                            if (leftTime >= 0) {
                                h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
                                m = Math.floor(leftTime / 1000 / 60 % 60);
                                s = Math.floor(leftTime / 1000 % 60);
                            } else {
                                //_this.orderDetail.status = '6';
                            }
                            if (d == 0 && h == 0 && m == 0 && s == 0) {
                                clearInterval(_this.timeObject);
                            }
                            if(h<10){
                                h='0'+h
                            }
                            if(m<10){
                                m='0'+m
                            }
                            if(s<10){
                                s='0'+s
                            }
                            _this.remainingTime = h+':'+m+":"+s
                        }, 1000);
}
Countdown()
posted @ 2018-02-23 16:25  Sarah119  Views(285)  Comments(0Edit  收藏  举报