[英]Javascript countdown timer can't set half past hour
I'm trying to use a countdown timer to count to a certain time everyday (monday to friday). 我正在尝试使用倒数计时器每天(星期一至星期五)进行计数。 So far everything works, except it can only be set to count to a certain hour (based on the 24 hour clock) without a half hour included. 到目前为止,所有功能都可以正常运行,除了只能将其设置为计数一个小时(基于24小时制)而不包括半小时。 So for example, if I wanted to count to 4PM, I'd set var target = 16;
因此,例如,如果我想计数到4PM,则将var target = 16;
设置var target = 16;
but if I wanted 4:30 and I tried to set var target = 1630;
但是如果我想4:30并尝试将var target = 1630;
设置var target = 1630;
it doesn't work. 它不起作用。 Unfortunately I don't have much experience with javascript, but I believe the problem is either with the way it's evaluating the target time using the getHours
function but not sure where to take it from there. 不幸的是,我对JavaScript没有太多的经验,但是我相信问题可能在于它使用getHours
函数评估目标时间的方式,但不确定从何处获取它。
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;
};
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
if (now.getHours() < target) { // don't do anything if we're past the cut-off point
var hrs = (target - 1) - now.getHours();
if (hrs < 0) hrs = 0;
var mins = 59 - now.getMinutes();
if (mins < 0) mins = 0;
var secs = 59 - now.getSeconds();
if (secs < 0) secs = 0;
var str = pad(hrs, 2) + ':' + pad(mins, 2) + '.<small>' + pad(secs, 2) + '</small>';
document.getElementById('countdownTimer').innerHTML = str;
}
}
}
var timerRunning = setInterval('countDown()', 1000);
}
If you're ok with considering another method, the following javascript will count down (in seconds) to any date in the future (and count up after the date has passed). 如果您可以考虑使用其他方法,则以下javascript会倒计时(以秒为单位),直到将来的任何日期(并在该日期过去之后递增)。
// new Date(year, month, day, hours, minutes, seconds, milliseconds)
var target = new Date(2014, 0, 30, 12, 30, 0, 0)
function countdown(id, targetDate){
var today = new Date()
targetDate.setDate(today.getDate())
targetDate.setFullYear(today.getFullYear())
targetDate.setMonth(today.getMonth())
var diffMillis = targetDate - today
if (diffMillis >= 0){
document.getElementById(id).innerHTML = millisToString(diffMillis)
}
}
setInterval(function(){countdown('seconds', target)},1000)
It uses the javascript date object, so you can literally use any date. 它使用javascript日期对象,因此您可以使用任何日期。
Updated example to do: 更新示例以执行以下操作:
Updated: updated code to override the targetDate to today's date. 更新:更新代码以覆盖targetDate到今天的日期。
Example: http://jsfiddle.net/kKx7h/5/ 示例: http : //jsfiddle.net/kKx7h/5/
对于4:30PM
,请尝试var target = 16.5
而不是var target = 1630;
You now need to add another variable for the minutes. 现在,您需要在分钟中添加另一个变量。 We can have target as an object literal rather than declare two variables for time: 我们可以将target作为对象文字,而不必为时间声明两个变量:
var target={hour: 15, minute:30};
if ( (now.getHours() < target.hour) && () now.getMinutes() < target.minute ){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.