Javascript 为什么不检查这种情况?
我现在正在做一个小项目,我想通过谷歌日历API检查房间是否繁忙。 为此,我使用此函数:Javascript 为什么不检查这种情况?,javascript,function,if-statement,google-calendar-api,console.log,Javascript,Function,If Statement,Google Calendar Api,Console.log,我现在正在做一个小项目,我想通过谷歌日历API检查房间是否繁忙。 为此,我使用此函数: //checking for change of all values. Then console.log values on change and executing request if busy. function avalabilityCheck() { [...inputs].forEach(input => { input.addEventListener('
//checking for change of all values. Then console.log values on change and executing request if busy.
function avalabilityCheck() {
[...inputs].forEach(input => {
input.addEventListener('change', function () {
if (date.value !== "" && startTime.value !== "" && endTime.value !== ""
) {let isBusy = true;
//looping through all rooms in compartment
for (let key in comp_1) {
if (comp_1.hasOwnProperty(key)) {
let calendarID = comp_1[key];
let roomName = key;
//console.log(value);
//user input that goes into the freebusy query
let requestBody = {
timeMin: date.value + "T" + startTime.value + ":00.000Z",
timeMax: date.value + "T" + endTime.value + ":00.000Z",
items: [
{
id: calendarID
}
],
timeZone: "GMT+01:00"
};
//make request to gcalendar if rooms are free. Giving back array on what times room is busy.
var freeRequest = gapi.client.calendar.freebusy.query(requestBody);
//executing request.
freeRequest.execute(function (resp) {
var responseObject = JSON.stringify(resp);
console.log(responseObject);
if (resp.calendars[calendarID].busy.length < 1) {
console.log(`${roomName} is free`);
} else { isBusy = false;
console.log("room is Busy");}
})
}
}
console.log("finito");
if (isBusy === false) {
console.log("working?");
}
else{console.log("not working");}
} else {
console.log("change date pls");
}
}
)
}
)
}
因此,当我运行应用程序时,控制台应给出:
console.log("finito");
if (isBusy === false) {
console.log("working?");
}
else{console.log("not working");}
但它只向控制台提供“finito”,显然没有检查isBusy的状态。
有人知道这里有什么问题吗?
谢谢大家! 首先,当房间很忙时,您将
isBusy
设置为false
(应该是true
)。您只需要在两个if
分支中的一个设置isBusy
,即使您正在初始化isBusy
,您也需要这样做,因为如果isBusy
由if
语句设置,然后if
语句再次运行并点击另一个分支?>如果条件if(响应日历[calendarID].busy.length<1)
未满,否则表示isBusy
应为false
,还是不正确?您的代码格式不正确。“finito”周围的部分缩进不正确,并且后面的语句与开始大括号位于同一行。重要的是,execute
回调只在稍后执行。因此,当这些回调都尚未执行时,您将获得“finito”输出。
console.log("finito");
if (isBusy === false) {
console.log("working?");
}
else{console.log("not working");}