我有一個回調函數一次加載一頁數據。當單擊後退和下一個鏈接時,該功能被調用/觸發。當最終用戶快速完成後續點擊下一個鏈接並重復調用加載函數時,我會遇到動畫和加載完成問題。我意識到這是一個異步調用,但有沒有辦法排隊或暫停加載/動畫,以便一個加載不會在另一個加載完成之前運行?防止在點擊事件中重複調用異步加載函數
function NextPageNav_Click() {
var CurPage = parseInt($('#CurPage').attr('value').toString()) + 1;
$('#CurPage').attr('value', CurPage);
loadPage(CurPage, 'Next');
}
function loadPage(CurPage, State) {
$.ajax({
type: "POST",
url: "defaulttmp3.aspx/GetLatestProfiles",
cache: true,
async: false,
data: "{'PageIndex': " + CurPage + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
switch (State) {
case 'Previous':
$('<div id="nextitems"></div>').insertBefore('#curitems');
$('#nextitems').empty();
$('#nextitems').setTemplateURL('page/public/templates/profiletemplate.htm');
$('#nextitems').processTemplate(msg);
$('#items').attr('style', 'left:-920px');
$('#items').animate({
"left": "+=920px"
}, 500, function() {
$('#curitems').remove();
$('#nextitems').attr('id', 'curitems');
}
);
break;
case 'Next':
$('<div id="nextitems"></div>').insertAfter('#curitems');
$('#nextitems').empty();
$('#nextitems').setTemplateURL('page/public/templates/profiletemplate.htm');
$('#nextitems').processTemplate(msg);
$('#items').animate({
"left": "-=920px"
}, 500, function() {
$('#curitems').remove();
$('#nextitems').attr('id', 'curitems');
$('#items').attr('style', 'left:0');
}
);
break;
default:
$('#curitems').empty();
$('#curitems').setTemplateURL('page/public/templates/profiletemplate.htm');
$('#curitems').processTemplate(msg);
break;
}
var RowsReturned = parseInt(msg.d.RowsReturned.toString());
var PageSize = parseInt(msg.d.PageSize.toString());
initPrevNextLinks(RowsReturned, PageSize, CurPage);
},
error: function (request, status, error) {
alert(request.statusText);
}
});
}