gpt4 book ai didi

javascript - JavaScript 中的 session 室可用性

转载 作者:行者123 更新时间:2023-11-30 08:03:11 24 4
gpt4 key购买 nike

我正在尝试为我所在大学的 session 室创建房间可用性脚本。我决定使用 jQuery 来实现它,方法是解析公共(public) Google 日历的 JSON 提要,然后在屏幕上显示房间是否可用。

我觉得自己很愚蠢,我已经为这个问题苦苦挣扎了 3 天,无论 Google 日历中是否有预约,脚本都说有空位。任何人都可以提出为什么会发生这种情况的建议。我不是程序员,我敢打赌这真的很简单,但我似乎看不到它。任何帮助将不胜感激!

关于 jsFiddle 的最小工作示例及以下:

<html>
<head>
<title>Graduate Center Conference Room</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script>
// Declare global variables

var events = [];
var currentReservation = null;
var nextReservation = null;
var gclaData = 'http://www.google.com/calendar/feeds/4occ2bc4m626a3pgmirlm06q5s%40group.calendar.google.com/public/full?orderby=starttime&sortorder=ascending&futureevents=true&singleevents=true&max-results=2&alt=json';

// Parse Google Calendar Public JSON Feed and store in the events global array

$(document).ready(function () {
$.getJSON(gclaData, function (data) {
$.each(data.feed.entry, function (i, entry) {
var dtStart = new Date(entry["gd$when"][0].startTime);
var dtEnd = new Date(entry["gd$when"][0].endTime);
var dtSummary = entry.content.$t;
var dtTitle = entry.title.$t;

events[i] = {
'start': dtStart,
'end': dtEnd,
'title': dtTitle,
'summary': dtSummary
};
});
});

reservationInfo = '';

// sort events just in case (JSON should be sorted anyways)

events.sort(function (a, b) {
return a.start - b.start;
});

// current date

var dtNow = new Date();

// let's assume there are no current room reservations unless script detects otherwise.
// No reservations indicated by -1
currentReservation = -1;

// loop through the events array and if current time falls between start and end of a element in the array the mark it as a reservation currently in progress

for (var i in events) {
if (dtNow >= events[i].start && dtNow <= events[i].end) currentReservation = i;
}

// Print the result to a output div

if (-1 == currentReservation) {

reservationInfo = '<h1>ROOM AVAILABLE</h1>';
$('#output').html(reservationInfo);

} else {

reservationInfo = '<h1>ROOM OCCUPIED</h1>';
$('#output').html(reservationInfo);
}
});
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>

最佳答案

一些观察...

1) 对您的代码进行一些重构,并始终进行一些调试!

2) 您的 events 变量不是预期的 object 因为 ajax 调用是 异步 和其他代码在进入将填充您的对象回调 之前执行。换句话说,您需要等待 ajax 调用,否则您的 object 将不是预期的 (可能是 undefined一开始和片刻之后,当 ajax 调用完成时,一个带有数据的对象)

只是要知道,您可以强制 ajax 调用同步,但这不是一个好方法。

试试这个:

我喜欢这样工作,代码更有条理:

现场演示: http://jsfiddle.net/oscarj24/8HVj7/


HTML:

<div id="output"></div>

jQuery:

/*
* http://stackoverflow.com/questions/23205399/conference-room-availability-in-javascript
* @author: Oscar Jara
*/

/* Google calendar URL */
var url = 'http://www.google.com/calendar/feeds/4occ2bc4m626a3pgmirlm06q5s%40group.calendar.google.com/public/full?orderby=starttime&sortorder=ascending&futureevents=true&singleevents=true&max-results=2&alt=json';

/* Status list used to show final message to UI */
var statusList = {
'ROOM_A': 'Available',
'ROOM_O': 'Occupied',
'ERROR_DATA': 'No data found at Google calendar.',
'ERROR_PROCESS': 'There was an error checking room availability.'
};

/* Document onReady handler */
$(document).ready(function () {
getCalData(url);
});

/*
* Get Google calendar data by request.
* @param {String} url
*/
function getCalData(url) {
var statusCode;
$.getJSON(url, function (data) {
if (!$.isEmptyObject(data)) {
var events = parseCalData(data);
var curReserv = getCurrentReservation(events);
statusCode = getRoomStatusCode(curReserv);
} else {
statusCode = 'ERROR_DATA';
}
printRoomStatusToUI(statusCode, $('#output'));
}).fail(function (r) { // HTTP communication error
console.error(r);
});
};

/*
* Parse Google calendar data.
* @param {Object} data
* @return {Object} events
*/
function parseCalData(data) {
var events;
events = $.map(data.feed.entry, function (evt, i) {
var dt = evt['gd$when'][0];
return {
start: new Date(dt.startTime),
end: new Date(dt.endTime),
title: evt.title.$t,
summary: evt.content.$t
};
});
if (events) {
sortEvents(events); // Just in case
}
return events;
};

/*
* Sort Google calendar events.
* @param {Object} events
*/
function sortEvents(events) {
events.sort(function (a, b) {
return a.start - b.start;
});
}

/*
* Get/check for current reservation.
* If current time falls between start and end of an event,
* mark it as a reservation currently in progress.
* @param {Object} events
* @return {int} curReserv
*/
function getCurrentReservation(events) {
var curReserv;
if (events) {
var dtNow = new Date(); // Current datetime
curReserv = -1; // No reservations
for (var i in events) {
var dtStart = events[i].start;
var dtEnd = events[i].end;
if (dtNow >= dtStart && dtNow <= dtEnd) {
curReserv = i;
break;
}
}
}
return curReserv;
};

/*
* Get room availability statusCode.
* @param {int} curReserv
* @return {String} statusCode
*/
function getRoomStatusCode(curReserv) {
var statusCode = 'ROOM_A';
if (!curReserv) {
statusCode = 'ERROR_PROCESS';
} else if (curReserv && curReserv != -1) {
statusCode = 'ROOM_O';
}
return statusCode;
};

/*
* @private
* Get room status text.
* @param {String} statusCode
* @return {String}
*/
function getRoomStatusText(statusCode) {
return statusList[statusCode];
};

/*
* @private
* Check if statusCode is an ERROR one.
* @param {String} statusCode
* @return {Boolean}
*/
function isErrorStatus(statusCode) {
return (statusCode.indexOf('ERROR') > -1);
};

/*
* Print room availability to UI.
* @param {String} statusCode
* @param {Object} elem
*/
function printRoomStatusToUI(statusCode, elem) {
var statusText = getRoomStatusText(statusCode);
var isError = isErrorStatus(statusCode);
if (statusText && $.trim(statusText) != '') {
if (!isError) {
statusText = '<h1>Room is: ' + statusText + '</h1>';
}
elem.html(statusText);
}
};

关于javascript - JavaScript 中的 session 室可用性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23205399/

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