【发布时间】:2018-12-28 14:44:13
【问题描述】:
尝试在每日发货截止时间之前建立每日“订购时间”倒计时。 (更喜欢保留 vanilla js)- 在这个特定的应用程序上,服务器端脚本不是一个选项。
1:我正在尝试确保如何将时间设置为特定的 EST 时间(每天下午 6 点),但不确定香草 JS 是否可以这样做?
2:当计时器倒计时并且还剩不到一小时时,它看起来像 0:mm:ss - 是否可以在它为 0 时隐藏小时而只显示 mm:ss?
FIDDLE: http://jsfiddle.net/bill9000/rwet0o5f/96/
if (document.getElementById('countdownTimer')) {
pad = function(n, len) { // leading 0's
var s = n.toString();
return (new Array( (len - s.length + 1) ).join('0')) + s;
};
var timerRunning = setInterval(
function countDown() {
var now = new Date();
if ( (now.getDay() >= 1) && (now.getDay() <= 5) ) { // Monday to Friday only
var target = 15; // 15:00hrs is the cut-off point -------- Trying to get 6pm EST regardless of user time zone
if (now.getHours() < target) { // would be best if could hide the whole counter if past cutoff point each day
var hrs = (target - 1) - now.getHours();
//if (hrs < 0) hrs = 0;
if (hrs = 0) '';
var mins = 59 - now.getMinutes();
if (mins < 0) mins = 0;
var secs = 59 - now.getSeconds();
if (secs < 0) secs = 0;
var str = 'Order in the next <strong>' + hrs + ':' + pad(mins, 2) + ':' + pad(secs, 2) + '</strong> to ship <strong>today</strong>.' ;
document.getElementById('countdownTimer').innerHTML = str;
}
}
}, 1000
);
}
【问题讨论】:
-
对于第一个问题,这个答案可能会对您有所帮助:stackoverflow.com/questions/36206260/…
-
如果不同的倒计时部分 > 0,则将它们存储在一个数组中。在构建
str时,使用.join(":")连接数组的不同部分:if (hrs > 0) { countdownParts.push(hrs); } if (mins >= 0) { countdownParts.push(pad(mins, 2)); } if (secs >= 0) { countdownParts.push(pad(secs, 2)); } str = /*...*/ + countdownParts.join(":") + /*...*/; -
if (hrs > 0 || mins > 0) { countdownParts.push(pad(mins, 2)); }...
标签: javascript timer countdown