Common examples of jquery work--ajaxstart and Ajaxstop (Ajaxcomplete)

Source: Internet
Author: User
Tags sleep function

In jquery, the Ajaxstart () method is called when an AJAX request is started, and there are no other incomplete AJAX requests. Similarly, the Ajaxstop () method is called when all AJAX requests are complete. The parameters of these methods are a function that will be called when the event occurs.

Each step of using these methods is to get a reference to a page element. You can then invoke the Ajaxstart () and Ajaxstop () methods on this element.

Use syntax:

("#loading"). Ajaxstart (function () {$ (this). Show ();//callback}): When an AJAX request starts, it displays the contents of the id "loading";

("#loading"). Ajaxstop (function () {$ (this). Hide ();//call}): When an AJAX request ends, the contents of the id "loading" are hidden;

In the process of development, I usually use them to do the page waiting for the progress of the picture display, which is loaded with Ajax and can not be loaded quickly (sometimes the page is not shown in a slow half a day), Then will automatically call Ajaxstart show a wait for the picture out (I will generally use wbox do popup a transparent layer), and so on the page all content Ajax loading complete, and then close the layer;

To facilitate your understanding, I would like to give you a simple example that I hope will help you:

First put the need to say, jquery library, a picture (waiting for the progress of the can, many online), OK, you can start:

The contents of the index.php file are as follows:

<script src= "Jquery.js" ></script>
<a href= "#" id= "O" >o</a><br/>
<a href= "#" id= "P" >p</a><br/>
<a href= "#" id= "Q" >q</a><br/>
<div id= "Loading" style= "Display:none;" ></div>
<div id= "Content" ></div>

<script>
$ (document). Ready (function () {

$ ("#loading"). Ajaxstart (function () {
$ (this). Show ();
}). Ajaxstop (function () {//ajaxstop change to Ajaxcomplete is the same
$ (this). Hide ();
});

$ ("#o"). Click (function () {
$.post ("For.php?id=o", function (data) {
$ ("#content"). HTML (data);
});
})

$ ("#p"). Click (function () {
$.post ("For.php?id=p", function (data) {
$ ("#content"). HTML (data);
});
})

$ ("#q"). Click (function () {
$.post ("For.php?id=q", function (data) {
$ ("#content"). HTML (data);
});
})

})
</script>

for.php content is as follows:

<?php
if ($_get[' id ')
{
for ($i =0; $i <3; $i + +)
{
Sleep (4);//For example we usually open the page very slowly, using the sleep function, so that Ajax loading time to wait 12 seconds;
echo $_get[' id '];
}
}

?>

OK, put the two files and the jquery library, the picture in your root directory under the same directory, access to index.php, point O or P or Q, you will see the wait progress picture, loading completed in <a> The following layer shows the contents of the Ajax loading OOO or PPP or QQQ and closes the picture waiting to load, detailed everyone can try, with more natural can appreciate the benefits of it, hehe ...

Finally, Ajaxstart and Ajaxstop are both global methods, regardless of where their code is created, and they execute as long as Ajax is executed. If Ajax is also used elsewhere on this page, the global function will do the same because they are global. If you want an AJAX to be unaffected by the global approach, you can set the global parameter to False in the $.ajax (options) method, such as:

$.ajax ({
URL: "Test.html",
Global:false
});

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.