gpt4 book ai didi

javascript - 使用 jQuery 将值动态插入注释字符串

转载 作者:行者123 更新时间:2023-12-03 04:37:19 26 4
gpt4 key购买 nike

如何将明天daystartTimeendTime插入到setupOrderingNotAvailable函数的内容消息中如果今天的 dayendTime 已过期?现在,setupOrderingNotAvailable 消息显示“...明天上午 11:00 到晚上 9:00 再次”,但并非每天都如此;这是我想插入明天时间的地方。

代码

var days = {
'1': {startTime: '4:00 PM', endTime: '8:00 PM'},
'2': {startTime: '11:00 AM', endTime: '9:00 PM'},
'3': {startTime: '11:00 AM', endTime: '9:00 PM'},
'4': {startTime: '11:00 AM', endTime: '4:00 PM'},
'5': {startTime: '11:00 AM', endTime: '10:00 PM'},
'6': {startTime: '12:00 PM', endTime: '10:00 PM'},
'7': {startTime: '12:00 PM', endTime: '8:00 PM'},
};

var curr_day = new Date().getDay();
var curr_time = getval();
var orderIsAvailable = false;
var day = days[curr_day];

//
function setupOrderingAvailable() {
document.querySelector('#alert-success').onclick = function() {
$.sweetModal({
content: '<h2>Online Ordering is Available!</h2>' +
'Please click the button below to begin your online order.' +
'<div class="sweet-modal-buttons"><a href="http://pizzospizzeria.hungerrush.com" target="_blank" class="button greenB">Start Order</a></div>',
// icon: $.sweetModal.ICON_SUCCESS
});
};
}

// Need to make this a function that inserts tomorrows startTime and endTime into message if todays endTime has passed
function setupOrderingNotAvailable() {
document.querySelector('#alert-success').onclick = function() {
$.sweetModal({
content: '<h2>Online Ordering is Not Available</h2>' +
'Online ordering will be available again tomorrow from 11:00am to 9:00pm',
buttons: [{
label: 'Close',
classes: 'redB'
}]
});
};
}
if (day) {
if (get24Hr(curr_time) > get24Hr(day.startTime) && get24Hr(curr_time) < get24Hr(day.endTime)) {
orderingIsAvailable = true;
} else {
document.querySelector('#alert-success').onclick = function() {
$.sweetModal({
content: '<h2>Online Ordering is Not Available</h2>' +
'Online ordering will be available again tomorrow from 11:00am to 9:00pm',
buttons: [{
label: 'Close',
classes: 'redB'
}]
});
}
}
}

if (orderingIsAvailable) {
setupOrderingAvailable();
} else {
setupOrderingNotAvailable();
}

最佳答案

我通过确定开始和结束时间的时间(以毫秒为单位)稍微清理了时间比较代码。这样可以更清晰地(IMO)比较现在的时间(作为数字)与开始时间和结束时间。 Date对象返回的日期整数范围是0-6,所以我也更新了它。

从那里您可以进行比较,如果现在无法订购

这是一个 fiddle ,尽管删除了 sweetModal 方面,但无论您如何显示它,都会创建相同的字符串。

https://jsfiddle.net/wx97g6x1/2/

var days = {
0: {
startTime: '4:00 PM',
endTime: '8:00 PM'
},
1: {
startTime: '11:00 AM',
endTime: '9:00 PM'
},
2: {
startTime: '11:00 AM',
endTime: '9:00 PM'
},
3: {
startTime: '11:00 AM',
endTime: '4:00 PM'
},
4: {
startTime: '11:00 AM',
endTime: '10:00 PM'
},
5: {
startTime: '12:00 PM',
endTime: '10:00 PM'
},
6: {
startTime: '12:00 PM',
endTime: '8:00 PM'
}
};

var now = new Date();
var time = now.getTime();
var day = days[now.getDay()];

var startToday = Date.parse(now.toDateString() + ' ' + day.startTime);
var endToday = Date.parse(now.toDateString() + ' ' + day.endTime);

function setupOrderingAvailable() {
console.log('Online Ordering is Available!');
}

function setupOrderingNotAvailable() {
var tomorrow = days[now.getDay() + 1];
console.log('Online ordering will be available again tomorrow from '+ tomorrow.startTime +' to '+ tomorrow.endTime)
}

if (time > startToday && time < endToday) {
setupOrderingAvailable();
} else {
setupOrderingNotAvailable();
}

关于javascript - 使用 jQuery 将值动态插入注释字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43239329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com