Skip to content

Instantly share code, notes, and snippets.

@JSoon
Created July 18, 2019 02:46
Show Gist options
  • Save JSoon/12f59449de6166130b9e37d0a5c95c78 to your computer and use it in GitHub Desktop.
Save JSoon/12f59449de6166130b9e37d0a5c95c78 to your computer and use it in GitHub Desktop.
ajax请求失败后重试封装方法
/**
* @description 带重试器的jQuery ajax封装方法
*
* @param {object} opts $.ajax options
*/
function JQ_AJAX(opts) {
var tryCount = opts.tryCount || 0; // 当前重试次数,默认0次
var retryLimit = opts.retryLimit || -1; // 当前最大重试次数,默认-1,请求失败不重试
(function request() {
$.ajax(opts)
.then(function () {
// success
}, function () {
// failure
tryCount++;
if (tryCount <= retryLimit) {
// try again
request();
}
});
})();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment