dMaller
dMaller

Reputation: 113

Where in this should I add an AJAX call for a load throbber?

Here's the function that is called when the user clicks a button that generates a shortened url and I'm wondering where I would use an AJAX call for the throbber. It takes a few seconds and I wanted to use a throbber in the mean time. Or would I end up creating a whole new function outside of this one that is called when the button is clicked? I haven't seen any other examples that look like my code. Really new here, would really appreciate any help, thanks.

html that I suppose I would need to link the gif with...

<img src="gif/Loader.gif" id="loader"/>

current js function I'm using to generate the short url function, it takes a couple seconds though so I think I need to call my gif in there somewhere and get rid of it when the callback is successful?

function getShare()
{       
        var s = document.createElement('script');
        var browserUrl = document.location.href;
        //alert(browserUrl);
        if (browserUrl.indexOf("?") != -1){
                browserUrl = browserUrl.split("?");
                browserUrl = browserUrl[0];
        }
        //alert(browserUrl);

        var gifUrl = document.getElementById('gif_input').value;
        var vidUrl = document.getElementById('vid_input').value;
        //alert(gifUrl + "|" + vidUrl);

        url = encodeURIComponent(browserUrl + "?gifVid=" + gifUrl + "|" + vidUrl);
        //alert(encodeURIComponent("&"));
        s.id = 'dynScript';
        s.type='text/javascript';
        s.src = "http://b1t.co/Site/api/External/MakeUrlWithGet?callback=resultsCallBack&url=" + url;
        document.getElementsByTagName('head')[0].appendChild(s);
}

function resultsCallBack(data)
{
    //document.getElementById("results").innerHTML = JSON.stringify(data);
    //alert(JSON.stringify(data));
    var obj = jQuery.parseJSON(JSON.stringify(data));
    //alert(obj.shortUrl);
    jQuery("#input-url").val(obj.shortUrl);
}

Upvotes: 1

Views: 457

Answers (2)

Nikita Holovach
Nikita Holovach

Reputation: 306

Add your image where it suppose to be displayed, but hide it:

<img src="gif/Loader.gif" id="loader" style="display:none;"/>

Here is function that take url and access API

function getShare(url)
{ 
    jQuery('#loader').show(); // show loading...
    jQuery.ajax({
        dataType: "jsonp",
        jsonpCallback:'apiCallback', // this will be send to api as ?callback=apiCallback because this api do not want to work with default jQuery callback function name
        url: 'http://b1t.co/Site/api/External/MakeUrlWithGet',
        data: {'url':url},
        success: function(response){
            jQuery('#loader').hide(); // hide loading...
            //respponse = {success: true, url: "http://sdfsdfs", shortUrl: "http://b1t.co/qz"} 
            if(response.success){

                console.log(response.url, response.shortUrl);

                jQuery("#input-url").val(response.shortUrl);
            }
        },
        error:function(){
            jQuery('#loader').hide(); // hide loading...
            //todo: network error o_O
        }
    });
}

By the way if you want to get value of HTML element use

var gifUrl = jQuery('#gif_input').val();

Instead of

var gifUrl = document.getElementById('gif_input').value;

Upvotes: 2

Michal Brašna
Michal Brašna

Reputation: 2323

Add throbber before appending script element to the DOM and remove it as first line in resultsCallback.

If you want handle error states as well, take a look at How to tell if a <script> tag failed to load.

Upvotes: 0

Related Questions