原来一直觉得ajax的defer方式已经是多余,现在才觉得asyn:false设置成同步方式都是有用的。打个比方,在ready页面加载又要保存切换选项卡状态又要保存前一搜索结果的话,就有用。
$.ajax(
{
type: "post",
url: "./HelpCenterTabSwitch.ashx",
data: "type=6&flag=1&tid=" + encodeURI(encodeURI(PTid)),
cache: false,
<span style="color:#ff0000;"> async: false,</span>
dataType: "text",
success: function (msg) {
<span style="background-color: rgb(204, 102, 204);">
$("#selection").show();
$("#UlContent li").remove();
$("#UlContent").append(msg);
</span>
}
});
}
if (window.localStorage.getItem("keyword") == "description" && typeof (flag) != "undefined") {
$("#problem").removeClass("at");
$("#description").addClass("at");
$.ajax(
{
type: "post",
url: "./HelpCenterTabSwitch.ashx",
data: "type=4&flag=1&tid=" + encodeURI(encodeURI(PTid)),
cache: false,
<span style="color:#ff0000;"> async: false,</span>
dataType: "text",
success: function (msg) {
<span style="color:#ffff00;"> </span><span style="color:#cc33cc;"> $("#selection").show();
$("#UlContent li").remove();
$("#UlContent").append(msg);</span>
}
});
}
if (window.sessionStorage.getItem("keyword2") != null&&window.sessionStorage.getItem("keyword2").replace(/(^\s*)|(\s*$)/g, "") != "" && window.sessionStorage.getItem("keyword2") != "请输入关键字搜索")
{
//$.ajax({
// type: "POST",
// anync: false,
// url: "HelpCenterSuggestion.ashx",
// cache: false,
// dataType: "text",
// data: { m: window.sessionStorage.getItem("keyword2") },
// success: function (result) {
$("#UlContent li").remove();
$("#UlContent li").removeAttr("padding");
$("#SearchResult").html("");
$("#SearchResult").html("搜索结果");
//$("#UlContent").append(result);
$("#UlContent").append(window.sessionStorage.getItem("searchResult2"));
$("#SearchResult").css("margin-top", "");
$("#SearchResult").css("margin-bottom", "");
$("#SearchResult").css("text-align", "");
$("#SearchResult").css("border-bottom", "1px solid #e2e3e7");
$("#selection").hide();
$("#content").show();
// }
//});
}
这边如果不设置为asyn为false的话,当执行前两段后的返回搜索结果最后还是会异步执行上面这段:
<span style="background-color: rgb(204, 102, 204);"> $("#selection").show();
$("#UlContent li").remove();
$("#UlContent").append(msg);
</span>