关于订单倒计时

102 阅读1分钟

倒计时 采用获取系统的时间戳 与订单时间进行获得diff 然后前端每秒去刷一个list 数组每次值减去1

这里需求是到了时候刷新一次

那么 这里为了避免过期订单 判断再 0-1000 内值存在时进行刷新

因为毫米时间戳少有概率是1000 整数 那么 对于负数就是过期订单。 大于1000 的正在倒计时

核心代码:

			startTimers() {
				const timer = setInterval(() => {
					console.log(this.remainingTimes);
					this.remainingTimes = this.remainingTimes.map(e => {
						return e - 1000;
					})
					// 如果有一个值是小于1000 就刷一下
					var check = this.remainingTimes.filter(e=>e>0 && e <1000)
					if(check.length){  this.page = 0;this.list = []; this.load()   }
				}, 10000);
				this.timers.push(timer);
			},
			clearTimers() {
				this.timers.forEach(timer => clearInterval(timer));
			},
			formatTime(time) {
				const seconds = Math.floor((time / 1000) % 60);
				const minutes = Math.floor((time / (1000 * 60)) % 60);
				const hours = Math.floor((time / (1000 * 60 * 60)) % 24);
				if (hours) {
					return `${hours}h ${minutes}m ${seconds}s`;
				} else {
					return `${minutes}m ${seconds}s`;
				}
			},