非异步POST调用

时间:2017-02-06 09:06:00

标签: javascript jquery ajax

我有以下工作并返回数据:

function ScrollToBottom() {
    $.ajax({
        async: false,
        type: "POST",
        url: "index.aspx/function",
        data: 'val,val2,val3',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function (response) {
            alert('An error has occured, please refresh the page.');
        },
        error: function (response) {
            alert('An error has occured, please refresh the page.');
        }
    });
}

function OnSuccess(response) {
    var xmlDoc = $.parseXML(response.d);
    var xml = $(xmlDoc);
}

问题出现在firefox中,因为async: false没有飞到那里。如果用户一直滚动到页面底部,则调用上述函数。

在Firefox中发生的是用户滚动,多次调用该函数,而不是说5组不同的数据,用户可以看到相同的数据集5次。

上面调用的替代方法是什么,所以它不会是异步和普通的同步“等到前一位完成”代码?

1 个答案:

答案 0 :(得分:1)

如果正在加载,请尝试设置标志:

var loading = false;
function ScrollToBottom() {
    if(loading) return;
    loading = true;
    $.ajax({
        async: false,
        type: "POST",
        url: "index.aspx/function",
        data: 'val,val2,val3',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function (response) {

            alert('An error has occured, please refresh the page.');
        },
        error: function (response) {
            alert('An error has occured, please refresh the page.');   
        },
        always: function(response){
            loading = false;
        }
    });
}

function OnSuccess(response) {
    var xmlDoc = $.parseXML(response.d);
    var xml = $(xmlDoc);
}