情景是: 在悬停按钮时,将看到报价即将到来的下一个可用时间。我用了代码
function closest (num, arr) {
var closestNext=10000;
for (var val = 0; val < arr.length; val++) {
if (arr[val] > num) {
if (arr[val] < closestNext)
{
closestNext = arr[val];
}
}
}
if (closestNext==10000){
closestNext=arr[0];
}
return closestNext;
}
这让我可以显示一天(我正在javascript中加载页面加载页面)。但现在的问题是,如果在每天早上10点到11点之间提出报价。优惠在关闭时如何显示数据? 精心准备: 假设每天下午4点提供报价。其余时间悬停在报价显示按钮上,显示“下一个报价为下午4点”。这我通过使用数组和当前时间来计算和显示解决了。现在问题是假设每个星期五上午10点都有报价,然后在结婚时它必须显示报价在“上午10点”。
答案 0 :(得分:1)
如果我能够正确理解您的问题,那么这里是下一个提议日期和时间并返回适当的消息的功能。这一天是1-7(星期一),时间是24小时格式。
function getNextOffer(offer_day, offer_time) //offer_day = 4, offer_time = 16
{
var msg = '';
var today = new Date();
var day_today = today.getDay();
var time_today = today.getHours()
if(offer_day != day_today) //different day
{
msg = "thurs " + offer_time; //this msg can be manipulated
}
else
{
if(time_today > offer_time) //time elapsed
{
msg = "next thurs " + offer_time;
}
else
{
msg = "next offer by " + offer_time;
}
}
return msg;
}
编辑:多个值改变了逻辑位
//example array of offers [Fri, 12:00pm], [Thurs, 1:00pm], [Sat, 7:00pm]
var days = [[5, 12], [4, 13], [6, 19]];
function getNextOffer(days)
{
var msg = '';
var today = new Date();
var day_today = today.getDay();
var time_today = today.getHours();
var pre_offer_day = 8;
var pre_offer_time = 25;
for(i=0; i < days.length; i++)
{
var offer_day = days[i][0];
var offer_time = days[i][1];
if(pre_offer_day >= offer_day)
{
if(offer_day > day_today)
{
msg = "next " + offer_day + " at " + offer_time;
pre_offer_day = offer_day;
pre_offer_time = offer_time;
}
else if(offer_day == day_today && offer_time > time_today)
{
msg = "next " + offer_day + " at " + offer_time;
pre_offer_day = offer_day;
pre_offer_time = offer_time;
}
}
}
\\if no more offers in this week set the default msg
if(pre_offer_day == 8 && pre_offer_time == 25)
{
msg = msg = "next offer by thurs 10am";
}
return msg;
}