亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

ajax延遲插件

zhangwang / 3048人閱讀

最近遇到需要做ajax延遲的需求,上一段代碼記錄下:

var XHRService = function(options) {
    this.options = options;
    this.reqTimer = null;
};

XHRService.prototype = {
    constructor: XHRService,

    req: function(options) {
        var that = this,
            defer = $.Deferred();

        clearTimeout(this.reqTimer);
        this.reqTimer = setTimeout(function() {
            that._req(options, defer);
        }, this.options.reqDelay);

        return defer.promise();
    },

    _req: function(options, defer) {
        var that = this;

        if (this.xhr) {
            this.xhr.abort();
        }

        if (typeof this.options.before === "function") {
            clearTimeout(this.loadingTimer);
            this.loadingTimer = setTimeout(function() {
                that.options.before();
            }, this.options.loadingDelay);
        }

        this.xhr = $.ajax(options).done(function(data) {
                defer.resolve(data);
            })
            .always(function(res, status, xhrObj) {
                clearTimeout(that.loadingTimer);

                if (typeof that.options.after === "function") {
                    that.options.after();
                }

                if (xhrObj === that.xhr) {
                    that.xhr = null;
                }
            });
    }
};
var xhr = new XHRService({
    reqDelay: 10,
    loadingDelay: 10,
    before: function() {
        console.log("show loading...");//顯示loadingbar
    },
    after: function() {
        console.log("hide loading...");//隱藏loadingbar
    }
});
 xhr.req({
        url: url,
        dataType: "json"
    }).done(function(data) {
        console.log("done!");
    });

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/86210.html

相關(guān)文章

  • performance-report頁面性能、資源、錯(cuò)誤、ajax,fetch請求上報(bào)插件 完善小巧

    摘要:只做頁面性能數(shù)據(jù)的采集和上報(bào),是比較完整和健全的數(shù)據(jù)上報(bào)插件,它可以幫你完成以下功能是比較完整和健全的數(shù)據(jù)上報(bào)插件,它可以幫你完成以下功能當(dāng)前頁面上一頁面當(dāng)前瀏覽器版本信息頁面性能數(shù)據(jù)信息,例如頁面加載時(shí)間,白屏?xí)r間,解析時(shí)間等當(dāng)前頁面 performance-report只做頁面性能數(shù)據(jù)的采集和上報(bào),是比較完整和健全的數(shù)據(jù)上報(bào)插件,它可以幫你完成以下功能: performance-r...

    testbird 評論0 收藏0
  • select2 和 ajax的坑

    select2,一款帶多選功能,樣式更加好看的select插件。 下方記錄坑,前方高能. 要用ajax發(fā)請求并且為select2賦初始值 $.ajax({ url : _ctx+/basInfo/listPsinfoData, data : page=1&rows=9999&flag=...

    VEIGHTZ 評論0 收藏0
  • select2 和 ajax的坑

    select2,一款帶多選功能,樣式更加好看的select插件。 下方記錄坑,前方高能. 要用ajax發(fā)請求并且為select2賦初始值 $.ajax({ url : _ctx+/basInfo/listPsinfoData, data : page=1&rows=9999&flag=...

    plokmju88 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<