最近遇到需要做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
摘要:只做頁面性能數(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...
select2,一款帶多選功能,樣式更加好看的select插件。 下方記錄坑,前方高能. 要用ajax發(fā)請求并且為select2賦初始值 $.ajax({ url : _ctx+/basInfo/listPsinfoData, data : page=1&rows=9999&flag=...
select2,一款帶多選功能,樣式更加好看的select插件。 下方記錄坑,前方高能. 要用ajax發(fā)請求并且為select2賦初始值 $.ajax({ url : _ctx+/basInfo/listPsinfoData, data : page=1&rows=9999&flag=...
閱讀 2158·2021-09-22 15:54
閱讀 1899·2021-09-04 16:40
閱讀 924·2019-08-30 15:56
閱讀 2686·2019-08-30 15:44
閱讀 2214·2019-08-30 13:52
閱讀 1179·2019-08-29 16:35
閱讀 3401·2019-08-29 16:31
閱讀 2624·2019-08-29 13:48