weixin_33736832 2017-02-06 09:05 采纳率: 0%
浏览 62

非异步POST调用

I have the following which works and returns data:

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);
}

The problem currently comes in firefox as the async: false doesn't fly there. The above function is called if the user has scrolled all the way to the bottom of the page.

What happens in Firefox is the user scrolls, the function is called several times and instead of say 5 different sets of data, the user sees the same set of data 5 times over.

What would be my alternative to the above call be, so it wouldn't be async and plain old synchronous "wait till the previous bit is done" code ?

  • 写回答

1条回答 默认 最新

  • 撒拉嘿哟木头 2017-02-06 10:58
    关注

    Try to set a flag if it is loading:

    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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向