这是我修改过的倒计时代码,
我希望倒计时不会显示零变体,如真正的倒计时系统,
和我希望它在java语法中而不是jquery libs!
但这部分代码无法正常工作有什么问题?
else if (weeks < 1 && day < 1) {
document.getElementById('countdown').innerHTML = hours + 'hrs '+ minutes + 'mins ' + seconds + 'secs';}
完整的HTML文档:
<!DOCTYPE html>
<html>
<body>
<script>
var end = new Date('Jan 29, 2017 15:37:25');
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var _week = _day * 7;
var timer;
function showRemaining() {
var now = new Date();
var distance = end - now;
if (distance < 0) {
clearInterval(timer);
document.getElementById('countdown').innerHTML = 'EXPIRED!';
return;
}
var weeks = Math.floor(distance / _week);
var days = Math.floor((distance % _week) / _day);
var hours = Math.floor((distance % _day) / _hour);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
if (weeks < 1) {
document.getElementById('countdown').innerHTML = days + 'days ' + hours + 'hrs '+ minutes + 'mins ' + seconds + 'secs';
}
else if (days < 1) {
var days = Math.floor(distance/ _day);
document.getElementById('countdown').innerHTML = days + 'days ' + hours + 'hrs '+ minutes + 'mins ' + seconds + 'secs';
}
else if (weeks < 1 && day < 1) {
document.getElementById('countdown').innerHTML = hours + 'hrs '+ minutes + 'mins ' + seconds + 'secs';
}
else { document.getElementById('countdown').innerHTML = weeks + 'weeks ' + days + 'days ' + hours + 'hrs '+ minutes + 'mins ' + seconds + 'secs';}
}
timer = setInterval(showRemaining, 1000);
</script>
<p id="countdown"></p>
</body>
</html>
答案 0 :(得分:0)
它无法达到你的if语句......
if (weeks < 1 )
会在“(&lt; 1&amp;&amp; days&lt; 1)”之前吞下......
如果(周&lt; 1&amp;&amp;天&lt; 1)comes first. Also you are using
天instead of
天. Also, don't declare your
天'两次,请切换你的if以进行测试...不好的做法,虽然它不会导致你的问题。
您更有可能想要删除该声明中的else
。
这就是你想要的:
if (weeks < 1) {
document.getElementById('countdown').innerHTML = days + 'days ' + hours + 'hrs '+ minutes + 'mins ' + seconds + 'secs';
}
else if (days < 1) {
days = Math.floor(distance/ _day);
document.getElementById('countdown').innerHTML = days + 'days ' + hours + 'hrs '+ minutes + 'mins ' + seconds + 'secs';
}
if (weeks < 1 && days < 1) {
document.getElementById('countdown').innerHTML = hours + 'hrs '+ minutes + 'mins ' + seconds + 'secs';
您遇到了许多问题 - 例如,如果您的分支机构中有day
而不是days
。粘贴此代码代替您对if的所有内容,您应该更接近您想要的内容。
以下是我对这些编码变化的看法:
10hrs 21mins 56secs
这里是用来代替你所拥有的代码:
<!DOCTYPE html>
<html>
<body>
<script>
var end = new Date('Jan 29, 2017 15:37:25');
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var _week = _day * 7;
var timer;
function showRemaining() {
var now = new Date();
var distance = end - now;
if (distance < 0) {
clearInterval(timer);
document.getElementById('countdown').innerHTML = 'EXPIRED!';
return;
}
var weeks = Math.floor(distance / _week);
var days = Math.floor((distance % _week) / _day);
var hours = Math.floor((distance % _day) / _hour);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
if (weeks < 1) {
document.getElementById('countdown').innerHTML = days + 'days ' + hours + 'hrs '+ minutes + 'mins ' + seconds + 'secs';
}
else if (days < 1) {
days = Math.floor(distance/ _day);
document.getElementById('countdown').innerHTML = days + 'days ' + hours + 'hrs '+ minutes + 'mins ' + seconds + 'secs';
}
if (weeks < 1 && days < 1) {
document.getElementById('countdown').innerHTML = hours + 'hrs '+ minutes + 'mins ' + seconds + 'secs';
}
else { document.getElementById('countdown').innerHTML = weeks + 'weeks ' + days + 'days ' + hours + 'hrs '+ minutes + 'mins ' + seconds + 'secs';}
}
timer = setInterval(showRemaining, 1000);
</script>
<p id="countdown"></p>
</body>
</html>