jQuery 倒数计时器在时间更改时打破周末时间
jQuery countdown timer breaks weekend times when time changed
我在博客 post 上找到了这段代码,它是一个使用 http://keith-wood.name/countdown.html 重复出现的 24 小时倒数计时器。我使用它的目的是作为第二天交货的截止日期,所以当下午 4 点的截止日期过去时,计时器会重置。周末不提供次日送达服务,因此他们是根据星期五、星期六或星期日调整计时器的计算。
这个 JsFiddle 展示了一个演示,它每天倒计时到下午 4 点,周末除外:https://jsfiddle.net/u3t2n9jk/6/
我的问题
我只需要将下午 4 点的截止日期更改为下午 3 点。如果我出于某种原因将 var deadline = 16 更改为 15(24 小时制),它会破坏周五、周六和周日的计算。我还需要做什么才能使其正常工作?
function nextDay() {
var target = new Date();
var deadline = 16; // Set 4pm/16:00hrs dispatch deadline
if (target.getHours() < deadline || (target.getHours() == 15 && target.getMinutes() == 59 ) ) {
console.log('Before deadline ' + target.getMinutes());
if ( target.getDay() == 5 && target.getHours() == 16 && target.getSeconds() > 1 ) { // For Friday After 4PM
target.setHours(88, 0, 0, 0);
}
else if ( target.getDay() == 6 ) { // For Saturday
target.setHours(64, 0, 0, 0);
}
else if ( target.getDay() == 7 ) { // For Sunday
target.setHours(30, 0, 0, 0);
}
else {
target.setHours(deadline, 0, 0, 0);
}
} else if (target.getHours() > deadline || (target.getHours() == deadline && target.getMinutes() >= 0) ) {
console.log('After deadline ' + target.getMinutes());
if ( target.getDay() == 5 && target.getHours() == 16 && target.getSeconds() > 1 ) { // For Friday After 4PM
target.setDate(target.getDate() + 1);
target.setHours(88, 0, 0, 0);
}
else if ( target.getDay() == 6 ) { // For Saturday
target.setDate(target.getDate() + 1);
target.setHours(64, 0, 0, 0);
}
else if ( target.getDay() == 7 ) { // For Sunday
target.setDate(target.getDate() + 1);
target.setHours(30, 0, 0, 0);
}
else {
target.setDate(target.getDate() + 1);
target.setHours(deadline, 0, 0, 0);
}
}
return target;
}
在一位同事的帮助下,最终重写了它背后的逻辑。请注意,您可以 copy/paste 此代码使其正常工作,但是您还需要 jQuery Countdown plugin:
function nextDay() {
var target = new Date();
var hours_now = target.getHours();
target.setHours(15);
target.setMinutes(0);
target.setSeconds(0);
// target is 3 today
if ( target.getDay() == 5 && hours_now >= 15 ) { // For Friday After 3PM
target.setDate(target.getDate() + 3); // target is 3 on Monday
}
else if ( target.getDay() == 6 ) { // For Saturday
target.setDate(target.getDate() + 2); // target is 3 on Monday
}
else if ( target.getDay() == 0 ) { // For Sunday
target.setDate(target.getDate() + 1); // target is 3 on Monday
}
else if (hours_now >= 15){ // Any other day and after deadline
target.setDate(target.getDate() + 1); // target is 3 Tomorrow
}
return target;
}
function reset() {
window.location.reload();
}
$('#timer').countdown({
until: nextDay(),
onExpiry: reset,
expiryText: '<div class="end">You have missed todays dispatch.</div>',
labels: ['Years', 'Months', 'Weeks', 'days', 'hrs', 'min', 's'],
labels1: ['Year', 'Month', 'Week', 'day', 'hr', 'min', 's'],
format: 'HMS'
});
我在博客 post 上找到了这段代码,它是一个使用 http://keith-wood.name/countdown.html 重复出现的 24 小时倒数计时器。我使用它的目的是作为第二天交货的截止日期,所以当下午 4 点的截止日期过去时,计时器会重置。周末不提供次日送达服务,因此他们是根据星期五、星期六或星期日调整计时器的计算。
这个 JsFiddle 展示了一个演示,它每天倒计时到下午 4 点,周末除外:https://jsfiddle.net/u3t2n9jk/6/
我的问题
我只需要将下午 4 点的截止日期更改为下午 3 点。如果我出于某种原因将 var deadline = 16 更改为 15(24 小时制),它会破坏周五、周六和周日的计算。我还需要做什么才能使其正常工作?
function nextDay() {
var target = new Date();
var deadline = 16; // Set 4pm/16:00hrs dispatch deadline
if (target.getHours() < deadline || (target.getHours() == 15 && target.getMinutes() == 59 ) ) {
console.log('Before deadline ' + target.getMinutes());
if ( target.getDay() == 5 && target.getHours() == 16 && target.getSeconds() > 1 ) { // For Friday After 4PM
target.setHours(88, 0, 0, 0);
}
else if ( target.getDay() == 6 ) { // For Saturday
target.setHours(64, 0, 0, 0);
}
else if ( target.getDay() == 7 ) { // For Sunday
target.setHours(30, 0, 0, 0);
}
else {
target.setHours(deadline, 0, 0, 0);
}
} else if (target.getHours() > deadline || (target.getHours() == deadline && target.getMinutes() >= 0) ) {
console.log('After deadline ' + target.getMinutes());
if ( target.getDay() == 5 && target.getHours() == 16 && target.getSeconds() > 1 ) { // For Friday After 4PM
target.setDate(target.getDate() + 1);
target.setHours(88, 0, 0, 0);
}
else if ( target.getDay() == 6 ) { // For Saturday
target.setDate(target.getDate() + 1);
target.setHours(64, 0, 0, 0);
}
else if ( target.getDay() == 7 ) { // For Sunday
target.setDate(target.getDate() + 1);
target.setHours(30, 0, 0, 0);
}
else {
target.setDate(target.getDate() + 1);
target.setHours(deadline, 0, 0, 0);
}
}
return target;
}
在一位同事的帮助下,最终重写了它背后的逻辑。请注意,您可以 copy/paste 此代码使其正常工作,但是您还需要 jQuery Countdown plugin:
function nextDay() {
var target = new Date();
var hours_now = target.getHours();
target.setHours(15);
target.setMinutes(0);
target.setSeconds(0);
// target is 3 today
if ( target.getDay() == 5 && hours_now >= 15 ) { // For Friday After 3PM
target.setDate(target.getDate() + 3); // target is 3 on Monday
}
else if ( target.getDay() == 6 ) { // For Saturday
target.setDate(target.getDate() + 2); // target is 3 on Monday
}
else if ( target.getDay() == 0 ) { // For Sunday
target.setDate(target.getDate() + 1); // target is 3 on Monday
}
else if (hours_now >= 15){ // Any other day and after deadline
target.setDate(target.getDate() + 1); // target is 3 Tomorrow
}
return target;
}
function reset() {
window.location.reload();
}
$('#timer').countdown({
until: nextDay(),
onExpiry: reset,
expiryText: '<div class="end">You have missed todays dispatch.</div>',
labels: ['Years', 'Months', 'Weeks', 'days', 'hrs', 'min', 's'],
labels1: ['Year', 'Month', 'Week', 'day', 'hr', 'min', 's'],
format: 'HMS'
});