How to show a loader while processing a javascript function?

Question:

I have a radio button that when clicked, executes a function that assembles a datepicker.

However, the mountDatepicker function is only loaded after a few seconds, after consulting three apis and then mounting the calendar.

So how do I load an image with a loader in place of the calendar while the function doesn't finish?

$("input[type='radio']").click(function(){
    if ($("input[name='diasaluguel']:checked").length > 0 ) {

    mountDatepicker();

    }
});

Answer:

function addLoader(el) {
    var gif = 'http://1.bp.blogspot.com/-Atj4ze_I-84/UeJk-CPeCsI/AAAAAAAAHno/UIbSZkTfWBA/s1600/loading.gif';
    var img = document.createElement('img');
    img.src = gif;
    el.innerHTML = '';
    el.appendChild(img);
}

Create a function to be run before mountDatepicker(); be called. You can do it differently: as I suggest, or already having a div in the HTML that overlaps the calendar.

As I did it adds an img element to the calendar and then removes it.

Example: http://jsfiddle.net/Sergio_fiddle/uxL5h57w/

Scroll to Top