如果我使用的语言不正确,请教我!! :)
我有一个when语句,该语句可以按预期工作,这就是说,延迟代码要等到getLocInfo返回它的承诺后才能执行:
var schoolID_alt;
var schoolLevel;
$.when(getLocInfo()).then(function() {
if (schoolLevel == '03') {
//Opportunities & Access
getAPResults();
//CCR
getCCRCareerReady();
getCCRCollegeReady();
getCCRReady();
}
else { //This is not a high school so hide the CCR section!!
$('.sl03').hide();
}
})
function getLocInfo() {
var parm = { schoolID_alt: schoolID_alt };
$.ajax({
type: "POST",
contentType: "application/json; charset=UTF-8",
url: "WebServices/myEquity.asmx/getSchoolInfo",
data: JSON.stringify(parm),
dataType: "json",
cache: false,
success: function (response) {
si = response.d;
$('#rptTitle').html(si.locationName + '<br/>School Profile');
$('#subTtl').html('Data Key: ' + si.locationName + ' (' + si.schoolLevelName + ')');
return schoolLevel = si.schoolLevel;
}
})
}
但是,如果我将其包装在另一个when函数中...新的when函数按预期工作,但是现在内部的“ when”内部以前运行的代码现在立即触发,而不是等待getLocInfo()履行诺言...有人可以帮忙解释我做错了什么吗?
$.when(schoolID_alt = getUrlParameter('schoolID_alt')).then(function () {
$.when(getLocInfo()).then(function() {
if (schoolLevel == '03') {
//Opportunities & Access
getAPResults();
//CCR
getCCRCareerReady();
getCCRCollegeReady();
getCCRReady();
}
else { //This is not a high school so hide the CCR section!!
$('.sl03').hide();
}
})
})
function getUrlParameter(sParam) {
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++) {
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam) {
return sParameterName[1];
}
}
}