-
Notifications
You must be signed in to change notification settings - Fork 12
/
jquery.retryAjax.js
44 lines (36 loc) · 1.48 KB
/
jquery.retryAjax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*! jQuery.retryAjax - v0.1.0 - 2012-09-17
* https://github.com/mberkom/jQuery.retryAjax/
* Copyright (c) 2012 Michael Berkompas; Licensed MIT */
(function($) {
$.retryAjax = function (ajaxParams) {
var errorCallback;
ajaxParams.tryCount = (!ajaxParams.tryCount) ? 0 : ajaxParams.tryCount;
ajaxParams.retryLimit = (!ajaxParams.retryLimit) ? 2 : ajaxParams.retryLimit;
ajaxParams.suppressErrors = true;
if (ajaxParams.error) {
errorCallback = ajaxParams.error;
delete ajaxParams.error;
} else {
errorCallback = function () {
};
}
ajaxParams.complete = function (jqXHR, textStatus) {
if ($.inArray(textStatus, ['timeout', 'abort', 'error']) > -1) {
this.tryCount++;
if (this.tryCount <= this.retryLimit) {
// fire error handling on the last try
if (this.tryCount === this.retryLimit) {
this.error = errorCallback;
delete this.suppressErrors;
}
//try again
$.ajax(this);
return true;
}
window.alert('There was a server error. Please refresh the page. If the issue persists, give us a call. Thanks!');
return true;
}
};
$.ajax(ajaxParams);
};
}(jQuery));