提问人:Hanny 提问时间:8/19/2021 更新时间:8/19/2021 访问量:36
回调中的 If 语句未正确执行 (Javascript) [duplicate]
If statement in a callback not executing properly (Javascript) [duplicate]
问:
我正在使用回调方法来等待ajax请求完成,以检查其他人是否锁定了项目。
callback 方法只是等待 ajax 请求完成,然后将生成的消息推送到 html 中。
下面是 ajax 函数:
function checkLockAndPushModalMessage(url, evaluationId, userIsObserver, callback) {
console.log('we are in checkandpush');
url = url + evaluationId + "/"
$.ajax({
url: url,
type: 'GET',
beforeSend: function (xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
},
success: function (data) {
if (userIsObserver){
console.log('user is observer (in success in check & push)')
if (data.employee_locked === true) {
callback(true);
} else {
callback(false);
}
}
if (!userIsObserver) {
console.log('user is non-observer (in success in check & push)')
if (data.admin_locked === true) {
callback(true);
} else {
callback(false);
}
}
},
error: function(jqxhr, textStatus, errorThrown) {
// TODO: Handle errors
console.log('There was an issue determining if the other use has locked the evaluation!');
},
})
};
回调方法是这样的:
function messageToModal(userHasLocked) {
let isLocked = userHasLocked;
console.log('in messageToModal');
console.log('other user has locked is: ' + userHasLocked);
console.log('isLocked is: ' + isLocked);
console.log(typeof userHasLocked);
if ('foo' != 'bar') {
// This evaluates fine and logs in console
console.log('Foo is NOT Bar!')
}
let modal_message = "NO NO NO";
if (isLocked === true) {
// The other user has locked the evaluation
let modal_message = "The other user has locked this evaluation.";
}
if (isLocked != true) {
// The other user has not locked the evaluation
let modal_message = "The other user has not locked this evaluation.";
}
// Push the message letting them the know the other users locked status to the modal
console.log('gotta push the message now: '+ modal_message);
$("#lockControlMessage").text( modal_message );
}
我这样称呼它:
checkLockAndPushModalMessage(evaluationUrl, evaluationData.id, userIsObserver, messageToModal);
里面有很多“虚拟数据”,这样我就可以看到发生了什么以及我们在控制台中的位置。
我在那里的另一个 if 语句的工作方式正如您所期望的那样。
控制台如下所示:
答: 暂无答案
评论
let
if
var
if