Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/omaxel/just-wait
A lightweight jQuery Ajax util library.
https://github.com/omaxel/just-wait
jquery jquery-ajax jquery-library jqxhr
Last synced: 4 days ago
JSON representation
A lightweight jQuery Ajax util library.
- Host: GitHub
- URL: https://github.com/omaxel/just-wait
- Owner: omaxel
- License: mit
- Created: 2018-06-13T14:37:01.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T01:33:58.000Z (about 2 years ago)
- Last Synced: 2024-12-18T08:30:51.434Z (about 2 months ago)
- Topics: jquery, jquery-ajax, jquery-library, jqxhr
- Language: JavaScript
- Homepage: https://omaxel.github.io/Just-Wait/
- Size: 1.12 MB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Just Wait
Wait what? The server response.[Just Wait](https://github.com/OmarMuscatello/Just-Wait) is a lightweight jQuery utility that allows you to specify a function to be executed after a specified amount of time from the start of the AJAX request. If the AJAX request ends before the specified amount of time, the function will never be executed.
Not clear? **Try the [**TEST TOOL**](https://omarmuscatello.github.io/Just-Wait/)**.
## Quick start
- Add the `just-wait.min.js` file to your page.
```
```
- *[Optional]* Adjust the `waitFor` parameter using `JustWait.options.waitFor` (see [Time adjusting](#time-adjusting)).
- Add a `wait` call back to the jqXHR object.
Example:
```
$.get('url') // or $.getJSON, $.post, $.getScript, $.load, $.ajax
.wait(() => { // <======= Wait callback
})
.done((data) => { ... })
.fail(() => { ... })
.always(() => {
// The AJAX request ends. Hide the loader.
$loader.hide();
});
```
- *[Optional]* Adjust the `waitFor` parameter only for a single request:
```
$.get({ url: 'url', waitFor: 500 }) // or $.getJSON, $.post, $.getScript, $.load, $.ajax
.wait(() => {
})
.done((data) => { ... })
.fail(() => { ... })
.always(() => {
// The AJAX request ends. Hide the loader.
$loader.hide();
});
```## Explanation
Suppose you have to display a loader while doing an AJAX request. I'm sure you are using a code similar to```
$loader.show(); // Show the loader before starting the AJAX request$.get('url') // or $.post or $.ajax
.done((data) => { ... })
.fail(() => { ... })
.always(() => {
// The AJAX request ends. Hide the loader.
$loader.hide();
});
```Of course, this is good: the user will see a loader while waiting for the server response. But, if the response is returned almost immediately the user will see a fast show/hide of the loader (aka flickering). You don't want that.
[**Just Wait**](https://github.com/OmarMuscatello/Just-Wait) solves this problem. It allows you to specify a function to be executed after a specified amount of time from the start of the AJAX request. If the AJAX request ends before the specified amount of time, the function will never be executed.
So, the previous code could be written as:```
$.get('url') // or $.post or $.ajax
.wait(() => {
// Show the loader after 100ms (default), if the request doesn't end before.
$loader.show();
})
.done((data) => { ... })
.fail(() => { ... })
.always(() => {
// The AJAX request ends. Hide the loader.
$loader.hide();
});
```See the below timeline to better understand how it works.
![Just Wait diagram](https://github.com/OmarMuscatello/Just-Wait/blob/master/resources/justwait-diagram.svg)
## Time adjusting
You can globally specify the amount of time to wait before the `wait` callback is executed by using the following code. Ensure that the code below is executed before any use of the `wait` callback.```
JustWait.options.waitFor = 100; // Default 100ms
```You can also specify the `waitFor` option for every request:
```
$.ajax({
url: 'url',
...
waitFor: 500 // ms
})
...
```
or
```
$.get({
url: 'url',
...
waitFor: 500 // ms
})
...
```
or
```
$.post({
url: 'url',
data: { id: 5 }
...
waitFor: 500 // ms
})
...
```
or [any shorthand for $.ajax](https://api.jquery.com/category/ajax/shorthand-methods/).