Display of loading when Ajax waits for data to return

Source: Internet
Author: User

Sometimes, when the amount of data processed by Ajax is large, the user waits for a long time. If the user is not prompted to wait at this time, the user may feel impatient. This section describes how to display loading when processing data in Ajax.

First, add a div layer on the HTML page:

<div id="loading" style="color:#ffffff; display:none; position:absolute; top:80px; left:3em;"></div> 

This div is not displayed at the beginning.

Then you can add the following code to the Ajax request function:

XmlReq. onreadystatechange = function () {var sliderBlank = document. getElementById ("sidebar"); // display the blank sliderBlank at the layer to display the result. innerHTML = ""; // obtain the loading display layer var loadingDiv = document. getElementById ("loading"); // Insert the loading graph loadingDiv. innerHTML = " 

That's easy!

A reference code is provided below:

xmlHttp.onreadystatechange = function(){//displace loading statusvar loadingDiv = document.getElementById("loading"); // get the divloadingDiv.innerHTML = "loading..."; // insert tip informationloadingDiv.style.right = "0"; // set position, the distance to the right border of current document is 0pxloadingDiv.style.top = "0"; // set position, the distance to the top border of current document is 0pxloadingDiv.style.display = ""; // display the div//load completedif(xmlHttp.readyState == 4) {//hide loading statusloadingDiv.style.display = "none"; // after completed, hidden the div againloadingDiv.innerHTML = ""; // empty the tip information//process responseif(xmlHttp.status == 200) {var str = xmlHttp.responseText;/* do something here ! */}elsealert("Error!" + "nStatus code is: " + xmlHttp.status + "nStatus text is: " + xmlHttp.statusText);}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.