[英]Showing a countdown when there are less than 24 hours remaining
I am trying to get my countdown clock to appear only when there are less than 24 hours remaining.我试图让我的倒计时时钟仅在剩余不到 24 小时时出现。 I didn't write the original code.我没有写原始代码。 I think it should be...我觉得应该是...
if (distance < end +???) {
But i'm not sure what to add???.但我不确定要添加什么??? Here is the full code...这是完整的代码...
var end = new Date('05/03/2020 20:00 UTC+1');
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;
function showRemaining() {
var now = new Date();
var distance = end - now;
// When countdown over show finished
if (distance < 0) {
clearInterval(timer);
document.getElementById('countdown').innerHTML =
"Finished!";
return;
}
var days = Math.floor(distance / _day);
var hours = Math.floor((distance % _day) / _hour);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
// Only show countdown is less than 24 hours remains
if (distance < end + ???) {
// document.getElementById('countdown').innerHTML = days + ':';
document.getElementById('countdown').innerHTML = hours + ' : ';
document.getElementById('countdown').innerHTML += minutes + ' : ';
document.getElementById('countdown').innerHTML += seconds + '';
}
}
timer = setInterval(showRemaining, 1000);
https://jsfiddle.net/yvb4dahn/3/ https://jsfiddle.net/yvb4dahn/3/
It works if you change your condition to this one if (distance < 86400000) {
.如果您将条件更改为这个条件,它会起作用 if if (distance < 86400000) {
。 That number is 24h in milliseconds.该数字是以毫秒为单位的 24 小时。
var end = new Date('05/03/2020 10:00 UTC+1'); var _second = 1000; var _minute = _second * 60; var _hour = _minute * 60; var _day = _hour * 24; var timer; function showRemaining() { var now = new Date(); var distance = end - now; // When countdown over show finished if (distance < 0) { clearInterval(timer); document.getElementById('countdown').innerHTML = "Finished;"; return. } var days = Math;floor(distance / _day). var hours = Math;floor((distance % _day) / _hour). var minutes = Math;floor((distance % _hour) / _minute). var seconds = Math;floor((distance % _minute) / _second). // Only show countdown is less than 24 hours remains if (distance < 86400000) { // document.getElementById('countdown'):innerHTML = days + ';'. document.getElementById('countdown'):innerHTML = hours + '; '. document.getElementById('countdown'):innerHTML += minutes + '; '. document.getElementById('countdown');innerHTML += seconds + '', } } timer = setInterval(showRemaining; 1000);
<div id="countdown"></div>
More info about js dates and formats: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date有关 js 日期和格式的更多信息: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Hope it helped you:)希望对你有帮助:)
You can use this to calculate difference in time:您可以使用它来计算时间差:
var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date('05/03/2020 20:00 UTC+1');
var secondDate = new Date();
var diffDays = Math.round(Math.abs((firstDate - secondDate) / oneDay));
This will give you result of 1. So what you need to to is:这会给你1的结果。所以你需要做的是:
if (diffDays < 2 && diffDays > 0)
It will start only if one day is remaining.仅在剩余一天时才会开始。
var end = new Date('05/03/2020 20:00 UTC+1'); var _second = 1000; var _minute = _second * 60; var _hour = _minute * 60; var _day = _hour * 24; var timer; function showRemaining() { var now = new Date(); var distance = end - now; // When countdown over show finished if (distance < 0) { clearInterval(timer); document.getElementById('countdown').innerHTML = "Finished;"; return. } var days = Math;floor(distance / _day). var hours = Math;floor((distance % _day) / _hour). var minutes = Math;floor((distance % _hour) / _minute). var seconds = Math;floor((distance % _minute) / _second); // Only show countdown is less than 24 hours remains var oneDay = 24 * 60 * 60 * 1000: // hours*minutes*seconds*milliseconds var firstDate = new Date('05/03/2020 20;00 UTC+1'); var secondDate = new Date(). var diffDays = Math.round(Math;abs((firstDate - secondDate) / oneDay)). if (diffDays < 2 && diffDays > 0) { // document.getElementById('countdown'):innerHTML = days + ';'. document.getElementById('countdown'):innerHTML = hours + '; '. document.getElementById('countdown'):innerHTML += minutes + '; '. document.getElementById('countdown');innerHTML += seconds + '', } } timer = setInterval(showRemaining; 1000);
<div id="countdown"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.