https://github.com/maoberlehner/retry-until-online
Delay the execution of a function until the browser is back online.
https://github.com/maoberlehner/retry-until-online
Last synced: 4 months ago
JSON representation
Delay the execution of a function until the browser is back online.
- Host: GitHub
- URL: https://github.com/maoberlehner/retry-until-online
- Owner: maoberlehner
- License: mit
- Created: 2016-09-30T15:02:33.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-05-20T14:19:57.000Z (about 7 years ago)
- Last Synced: 2025-03-05T13:11:34.527Z (4 months ago)
- Language: JavaScript
- Homepage:
- Size: 241 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# retry-until-online
[](https://www.patreon.com/maoberlehner)
[](https://paypal.me/maoberlehner)
[](https://travis-ci.org/maoberlehner/retry-until-online)
[](https://coveralls.io/github/maoberlehner/retry-until-online?branch=master)
[](https://github.com/maoberlehner/retry-until-online)Delay the execution of a function until the browser is back online.
Imagine a user is traveling by train and browsing your webshop on his phone. He finds what he is looking for and taps the "Buy now" button. But suddenly: a tunnel. The connection is lost and the AJAX request (for putting the product in the cart) fails.
By wrapping AJAX requests with the `retryUntilOnline` function you can prevent failing AJAX requests. Like in the tunnel example, the connection may be only lost for some seconds `retryUntilOnline` will execute the AJAX request as soon as the connection is restored.
## Usage
```bash
# Install the module.
npm install retry-until-online --save
``````js
// Load the module.
import retryUntilOnline from 'retry-until-online';// Delay the execution of a jQuery AJAX request until the browser is back online.
retryUntilOnline({ callback: () => $.ajax({ url: 'test.html' }) })
.then((value) => {
// This is called after the callback function was called,
// `value` is the return value of the callback function.
})
.catch((value) => {
// This is called when `maxTries` is reached and the offline
// callback function was called, `value` is the return value
// of the offline callback function.
});
```### Options
```js
import retryUntilOnline from 'retry-until-online';const options = {
callback: () => undefined, // Callback function which is called if online.
offlineCallback: () => undefined, // Callback function that is executed if `tries` reaches 0.
interval: 500, // Interval for checking the online status.
maxTries: -1, // How often the online status should be checked before giving up (-1 = unlimited).
};retryUntilOnline(options);
```## About
### Author
Markus Oberlehner
Website: https://markus.oberlehner.net
Twitter: https://twitter.com/MaOberlehner
PayPal.me: https://paypal.me/maoberlehner
Patreon: https://www.patreon.com/maoberlehner### License
MIT