Jump to content

showing loading image on ajax call


stijn0713

Recommended Posts

Hello, i've been looking around on how to show a loading image on ajax call but either the code doesnt work or either, i cant test the loading image since the ajax call is too fast to show.

 

can sombody give me suggestions?

 

i work with a javascript functions that uses the responseText utility and that is triggered by an onblur event.

 

what i tested so far:

 

so my function populates the city and state on zipcode entry. I've put this in the function

 

$("#city|| state").empty().html('<img src="/Images/ajax-loader.gif" />');   

    || didn't work

 

 

another thing:

 

if (xmlhttp.readyState==4) {
while (xmlhttp.readyState == 3) { 
}
}

 

didn't work either.

 

Another aspect, to preload an image:

 

could this be possible ?

 

if (document.images){
preload_image.src ="/Images/ajax-loader.gif";
}

 

Any better solutions to preload images?

 

Any help welcome.

 

Thanks in advance

 

 

 

 

 

Link to comment
Share on other sites

but the div i show my loader image is not the same as where the content of responseText comes. so when i use your example the image stays displayed for eternally.

 

function fillcitystate(controlname)

{

var zipstring = "";

xmlhttp = new XMLHttpRequest();

xmlhttp.open("GET", "postcode.php?postcode=" + controlname.value, true);

xmlhttp.onreadystatechange=function()

{

if (xmlhttp.readyState==4)

{

var zipstring = xmlhttp.responseText;

if (zipstring!="Error")

{

var ziparray = zipstring.split("|");

document.getElementById("gemeente").value = ziparray[1];

document.getElementById("provincie").value = ziparray[2];

}

}

else if (xmlhttp.readyState==3){

document.getElementById("load").innerHTML = '<img src="Images/ajax-loader.gif" border="0" alt="Loading, please wait..." />';

}

}

xmlhttp.send(null);

 

 

 

Link to comment
Share on other sites

And if i use :

 

while (xmlhttp.readyState!==4){

document.getElementById("load").innerHTML = '<img src="Images/ajax-loader.gif" border="0" alt="Loading, please wait..." />';

}

 

It eats my CPU and my browser gets stuck :S

 

What the f* is wrong ?

 

Link to comment
Share on other sites

<< Moving this thread to appropriate forum >>

 

And if i use :

 

while (xmlhttp.readyState!==4){

document.getElementById("load").innerHTML = '<img src="Images/ajax-loader.gif" border="0" alt="Loading, please wait..." />';

}

 

It eats my CPU and my browser gets stuck :S

 

Right, because it is basically repeating that process as quickly as it can over and over again until the condition is false. All  you need to do is set the loading image ONCE as soon as the AJAX call is made. Then once the response is returned from the call remove the image.

 

I'm sure there are a ton of resources out there to show how to do this, but here is some sample code that should work, but I'm not going to test it

 

function fillcitystate(controlname)
{   
    var zipstring = "";
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", "postcode.php?postcode=" + controlname.value, true);
    //Set the loading image
    document.getElementById("load").innerHTML = '<img src="Images/ajax-loader.gif" border="0" alt="Loading, please wait..." />';
    xmlhttp.onreadystatechange=function()
    {   
        if (xmlhttp.readyState==4)
        {
            //Remove the loading image
            document.getElementById("load").innerHTML = '';

            var zipstring = xmlhttp.responseText;
            if (zipstring!="Error")
            {
                var ziparray = zipstring.split("|");
                document.getElementById("gemeente").value = ziparray[1];
                document.getElementById("provincie").value = ziparray[2];
            }
            else
            {
                //Provide an error message
            }
        }
    }
    xmlhttp.send(null);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.