laravel刀片内的倒数计时

问题描述 投票:-1回答:1

enter image description here

[大家好,我正在尝试寻找一种方法来减少Time Left列的计数。我想通过ajax进行操作,并每分钟更新一次,但是我想它将影响性能。实现此目标的最佳方法是什么?我需要它来显示该列左侧的实时时间,并更新数据库中的值。谢谢!

laravel laravel-5 timer laravel-blade countdown
1个回答
0
投票

您不必每次都更新数据库中剩余的时间,我认为您甚至不需要存储它。您需要存储截止日期(结束日期),然后倒计时将仅在使用JavaScript的客户端进行,如下所示:

let timer = function (date) {
    let timer = Math.round(new Date(date).getTime()/1000) - Math.round(new Date().getTime()/1000);
		let minutes, seconds;
		setInterval(function () {
			days = parseInt(timer / 60 / 60 / 24, 10);
			hours = parseInt((timer / 60 / 60) % 24, 10);
			minutes = parseInt((timer / 60) % 60, 10);
			seconds = parseInt(timer % 60, 10);

			days = days < 10 ? "0" + days : days;
			hours = hours < 10 ? "0" + hours : hours;
			minutes = minutes < 10 ? "0" + minutes : minutes;
			seconds = seconds < 10 ? "0" + seconds : seconds;

			document.getElementById('cd-days').innerHTML = days;
			document.getElementById('cd-hours').innerHTML = hours;
			document.getElementById('cd-minutes').innerHTML = minutes;
			document.getElementById('cd-seconds').innerHTML = seconds;

			if (--timer < 0) {
				timer = duration;
			}
		}, 1000);
	}
 
//using the function
const today = new Date()
const tomorrow = new Date(today)
tomorrow.setDate(tomorrow.getDate() + 1)
console.log(tomorrow)
timer(tomorrow);
<span id="cd-days">0</span> Days 
<span id="cd-hours">0</span> Hours
<span id="cd-minutes">0</span> Minutes
<span id="cd-seconds">0</span> Seconds
© www.soinside.com 2019 - 2024. All rights reserved.