Skip to content

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Jan 22, 2023
2 parents 96e2c9b + 590aab2 commit 59f3e61
Show file tree
Hide file tree
Showing 3 changed files with 1,583 additions and 8 deletions.
12 changes: 4 additions & 8 deletions AJAXRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ Object.defineProperties(AJAXRequest, {

Object.defineProperties(AJAXRequest.META, {
VERSION: {
value: '2.1.1',
value: '2.1.2',
writable: false
},
REALSE_DATE: {
value: '2022-09-07',
value: '2022-09-14',
writable: false
},
CONTRIBUTORS: {
Expand Down Expand Up @@ -320,7 +320,7 @@ function AJAXRequest(config = {
this.log('AJAXRequest: Ready State = 3 (LOADING)', 'info');
} else if (this.readyState === 4 && this.status === 0) {
this.log('AJAXRequest: Ready State = 4 (DONE)', 'info');
this.active = false;

if (this.retry.times !== 0 && this.retry.pass_number < this.retry.times) {
this.log('AJAXRequest: Retry after '+this.retry.wait+' seconds...', 'info');
var i = this;
Expand All @@ -341,19 +341,15 @@ function AJAXRequest(config = {
}
} else if (this.readyState === 4 && this.status >= 200 && this.status < 300) {
this.log('AJAXRequest: Ready State = 4 (DONE).', 'info');
this.active = false;
setProbsAfterAjax(this, 'success');
} else if (this.readyState === 4 && this.status >= 400 && this.status < 500) {
this.log('AJAXRequest: Ready State = 4 (DONE).', 'info');
this.active = false;
setProbsAfterAjax(this, 'clienterror');
} else if (this.readyState === 4 && this.status >= 300 && this.status < 400) {
this.log('AJAXRequest: Ready State = 4 (DONE).', 'info');
this.active = false;
this.log('Redirect', 'info', true);
} else if (this.readyState === 4 && this.status >= 500 && this.status < 600) {
this.log('AJAXRequest: Ready State = 4 (DONE).', 'info');
this.active = false;
setProbsAfterAjax(this, 'servererror');
} else if (this.readyState === 4) {
this.active = false;
Expand Down Expand Up @@ -400,7 +396,6 @@ function AJAXRequest(config = {
}
function setProbsAfterAjax(inst, pool_name) {
//inst is of type XMLHTTPRequest
console.log(inst.url);
var headers = getResponseHeadersObj(inst);
var p = 'on' + pool_name + 'pool';
try {
Expand Down Expand Up @@ -453,6 +448,7 @@ function AJAXRequest(config = {
callOnErr(inst, jsonResponse, headers, e);
}
}
inst.active = false;
inst.log('AJAXRequest: Finished AJAX Request.', 'info');
}
/**
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ A light weight JavaScript class library that can help in making AJAX requests mu
* <a href="#custom-id">Custom ID</a>
* <a href="#enabling-or-disabling">Enabling or Disabling</a>
* <a href="#binding-properties">Binding Properties</a>
* <a href="#retry">Retry</a>
* <a href="#verbose-mode">Verbose Mode</a>
* <a href="#api-reference">API Docs</a>
* <a href="#usage-examples">Usage Examples</a>
Expand Down Expand Up @@ -639,6 +640,21 @@ ajax.bind(o1, null, 'success');

//Bind with all callback with ID = 'AJAX Success' on the pool 'success'.
ajax.bind(o1, 'AJAX Success', 'success');
```
## Retry
One of the features of the library is to have it retry to send AJAX request in case there was no internet connection. The developer can use the method `AJAXRequest.setRetry()` to specify retry attributes. The method has 4 parameters, `times`, `timeBetweenEachTryInSeconds`, `func` and , `props`. The parameter `times` represents how many times to attempt AJAX request before stopping. The parameter `timeBetweenEachTryInSeconds` represents number of seconds to wait before attemting to send the request again. The `func` is a callback to execute in case of retry event. The last parameter is an object that holds extra parameters to pass to the callback.
The callback will have two parameters, first one is number of seconds remaining before retrying and, second one is pass number (current retry attempt number).
The following code sample shows basic use case for this functionality.
``` javascript

ajax.setRetry(4, 14, function (remainingSec, passNum) {
console.log('Pass number: '+passNum);
console.log('Retry after '+remainingSec);
});

```
## Verbose Mode
Expand Down
Loading

0 comments on commit 59f3e61

Please sign in to comment.