vue--js實現倒計時

data(){
	return{
		isEnd:false,//倒計時是否結束
        endTime:'2018-10-29 08:00:00',//應爲接口獲取到的結束時間
		}
	},
created(){
		let that = this;
		that.setEndTime();
	},
methods:{
setEndTime(){
		var that = this;
		var interval = setInterval(function timestampToTime(){
		var date = (new Date(that.endTime)) - (new Date()); //計算剩餘的毫秒數
		if(date == 0){
			that.isEnd = true;
			clearInterval(interval)
		}else{
			that.time.D = parseInt(date / 1000 / 60 / 60 / 24 , 10);
			that.time.h = parseInt(date / 1000 / 60 / 60 % 24 , 10);
			if(that.time.h < 10){
				 that.time.h = "0" + that.time.h
			}
			that.time.m = parseInt(date / 1000 / 60 % 60, 10);//計算剩餘的分鐘
			if(that.time.m < 10){
				 that.time.m = "0" + that.time.m
			} 
	  		that.time.s = parseInt(date / 1000 % 60, 10);//計算剩餘的秒數 
	  		if(that.time.s < 10){
				 that.time.s = "0" + that.time.s
			}
			return that.time.D+that.time.h+that.time.m+that.time.s;	
		}
	},1000);
},
},

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章