精确的js每日重复倒数计时器
precise js daily repeat countdown timer
我有这个计时器,如果我想让它倒计时到一个小时,它就可以正常工作,比如 09:00。但是每天都要倒数到9:15,然后重新设置。
现在这是我的代码目前的样子:
<script>
var date;
var display = document.getElementById('time');
setInterval(function()
{
date = new Date();
var currentHours = date.getHours();
var currentMinutes = date.getMinutes();
var hours;
var minutes;
var seconds;
if(currentHours == 09 && currentMinutes < 15)
{
minutes = 14 - date.getMinutes();
seconds = 60 - date.getSeconds();
display.innerHTML = 'Tid til næste bestilling: ' + minutes + ' minutter, ' + seconds + ' sekunder.';
}
else if(currentHours == 09 && currentMinutes > 15)
{
hours = 08 + (24 - currentHours);
minutes = 74 - date.getMinutes();
seconds = 60 - date.getSeconds();
display.innerHTML = 'Tid til næste bestilling: ' + hours + ' timer, ' + minutes + ' minutter, ' + seconds + ' sekunder.';
}
else if(currentHours < 09)
{
hours = 08 - currentHours;
minutes = 60 - date.getMinutes();
seconds = 60 - date.getSeconds();
display.innerHTML = 'Tid til næste bestilling: ' + hours + ' timer, ' + minutes + ' minutter, ' + seconds + ' sekunder.';
}
else if(currentHours > 09)
{
hours = 08 + (24 - currentHours);
minutes = 60 - date.getMinutes();
seconds = 60 - date.getSeconds();
display.innerHTML = 'Tid til næste bestilling: ' + hours + ' timer, ' + minutes + ' minutter, ' + seconds + ' sekunder.';
}
else if(currentHours == 09 && currentMinutes == 15)
{
display.innerHTML = 'Bestillinger afsendt. Timeren starter om lidt igen.';
}
}
,1000);
</script>
现在我知道有一种更有效的方法可以做到这一点,但我似乎无法弄清楚。
非常感谢任何帮助。
你可以找到一个更容易实现的希望对你有所帮助。您可以随时设置。请注意,这只适用于一天,而且是 24 小时系统。即使月份发生变化,这仍然有效。
如果您有任何问题,请告诉我。
<script>
var date;
var display = document.getElementById('time');
setInterval(function()
{
date = new Date();
date1 = new Date();
// you can set the hours, minutes, seconds here this is the date co compare
date1.setHours(9);
date1.setMinutes(15);
date1.setSeconds(0);
// here you make a date diff
var diff =0;
if(date1 < date){
console.log('true');
date1.setDate(date1.getDate()+1);
diff = date1-date;
}else{
console.log('false');
diff = date1-date;
}
// to avoid divided by 0
if(diff ==0){
display.innerHTML = date+ ''+ date1 + ''+ date2+ ' Tid til næste bestilling: 24 timer, 0 minutter, 0 sekunder.';
return;
}
// calculate hours, minutes, seconds
hours = Math.floor(diff/(1000*60*60));
diff -= hours *1000*60*60
minutes = Math.floor(diff/(1000*60));
diff -= minutes *1000*60;
seconds = Math.floor(diff/(1000));
//finaly display
display.innerHTML = d' Tid til næste bestilling: ' + hours + ' timer, ' + minutes + ' minutter, ' + seconds + ' sekunder.';
}
,1000);
我有这个计时器,如果我想让它倒计时到一个小时,它就可以正常工作,比如 09:00。但是每天都要倒数到9:15,然后重新设置。 现在这是我的代码目前的样子:
<script>
var date;
var display = document.getElementById('time');
setInterval(function()
{
date = new Date();
var currentHours = date.getHours();
var currentMinutes = date.getMinutes();
var hours;
var minutes;
var seconds;
if(currentHours == 09 && currentMinutes < 15)
{
minutes = 14 - date.getMinutes();
seconds = 60 - date.getSeconds();
display.innerHTML = 'Tid til næste bestilling: ' + minutes + ' minutter, ' + seconds + ' sekunder.';
}
else if(currentHours == 09 && currentMinutes > 15)
{
hours = 08 + (24 - currentHours);
minutes = 74 - date.getMinutes();
seconds = 60 - date.getSeconds();
display.innerHTML = 'Tid til næste bestilling: ' + hours + ' timer, ' + minutes + ' minutter, ' + seconds + ' sekunder.';
}
else if(currentHours < 09)
{
hours = 08 - currentHours;
minutes = 60 - date.getMinutes();
seconds = 60 - date.getSeconds();
display.innerHTML = 'Tid til næste bestilling: ' + hours + ' timer, ' + minutes + ' minutter, ' + seconds + ' sekunder.';
}
else if(currentHours > 09)
{
hours = 08 + (24 - currentHours);
minutes = 60 - date.getMinutes();
seconds = 60 - date.getSeconds();
display.innerHTML = 'Tid til næste bestilling: ' + hours + ' timer, ' + minutes + ' minutter, ' + seconds + ' sekunder.';
}
else if(currentHours == 09 && currentMinutes == 15)
{
display.innerHTML = 'Bestillinger afsendt. Timeren starter om lidt igen.';
}
}
,1000);
</script>
现在我知道有一种更有效的方法可以做到这一点,但我似乎无法弄清楚。 非常感谢任何帮助。
你可以找到一个更容易实现的希望对你有所帮助。您可以随时设置。请注意,这只适用于一天,而且是 24 小时系统。即使月份发生变化,这仍然有效。
如果您有任何问题,请告诉我。
<script>
var date;
var display = document.getElementById('time');
setInterval(function()
{
date = new Date();
date1 = new Date();
// you can set the hours, minutes, seconds here this is the date co compare
date1.setHours(9);
date1.setMinutes(15);
date1.setSeconds(0);
// here you make a date diff
var diff =0;
if(date1 < date){
console.log('true');
date1.setDate(date1.getDate()+1);
diff = date1-date;
}else{
console.log('false');
diff = date1-date;
}
// to avoid divided by 0
if(diff ==0){
display.innerHTML = date+ ''+ date1 + ''+ date2+ ' Tid til næste bestilling: 24 timer, 0 minutter, 0 sekunder.';
return;
}
// calculate hours, minutes, seconds
hours = Math.floor(diff/(1000*60*60));
diff -= hours *1000*60*60
minutes = Math.floor(diff/(1000*60));
diff -= minutes *1000*60;
seconds = Math.floor(diff/(1000));
//finaly display
display.innerHTML = d' Tid til næste bestilling: ' + hours + ' timer, ' + minutes + ' minutter, ' + seconds + ' sekunder.';
}
,1000);