Giter Site home page Giter Site logo

just-wait's Introduction

Just Wait

Wait what? The server response.

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.

Quick start

  • Add the just-wait.min.js file to your page.
    <script src="just-wait.min.js"></script>
    
  • [Optional] Adjust the waitFor parameter using JustWait.options.waitFor (see 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 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

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.

just-wait's People

Contributors

dependabot[bot] avatar omaxel avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

soonbok

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.