Created
July 18, 2019 02:46
-
-
Save JSoon/12f59449de6166130b9e37d0a5c95c78 to your computer and use it in GitHub Desktop.
ajax请求失败后重试封装方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @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