Ajax Request Asynchronous Queue loading
when we develop a program, we usually run into situations where we use AJAX to load the data and display it to the list. Ajax uses asynchronous loading (async:true) by default. Why not use synchronization because AJAX synchronization loading will cause the UI to render thread blocking issues. This is usually the case when loading a large amount of data because the load time is too long to cause the page not to click, GIF animation die and browser crashes, and so on. So, generally, try to use Ajax asynchronously to load.
However, some of our requirements require Ajax to load synchronously, one loading and reloading the next, the so-called queue. As we have said before, synchronous loading can cause UI render blocking problems. So how do we implement sequential loading without causing the problem?
Sample code One:
<script src= "Http://libs.baidu.com/jquery/2.0.0/jquery.min.js" ></script>
<script>
$ ( function () {
$ ("#clickBtn"). On ("click", Function () {
getData (0,10);//Output 0-9
})
})
function GetData (i,length) {
$.ajax ({
type: "Post",
URL: "setdeffer.php",
data:{
data:i
},
async:true,//Asynchronous
success:function (data) {
$ ("#showArea"). Text ($ ("#showArea"). Text () +data+ "\ n");
if (i<length-1) {
getData (i+1,length);}}}
);
</script>
PHP Background code:
<?php
$str = $_post["Data"];
Sleep (1);//delay 1 seconds
echo "Output". $str;
>
Of course, jquery also provides us with the deferred object to solve the callback function problem.
Example code two:
<script src= "Http://libs.baidu.com/jquery/2.0.0/jquery.min.js" ></script>
<script>
$ ( function () {
$ ("#clickBtn"). On ("click", Function () {
recycledata (0,10);//Output 0-9
})
})
function GetData (i) {
var defer = $. Deferred ();
$.ajax ({
type: "Post",
URL: "setdeffer.php",
data:{
data:i
},
async:true,//asynchronous
success:function (data) {
$ ("#showArea"). Text ($ ("#showArea"). Text () +data+ "\ n");
Defer.resolve (data);
}
);
return Defer.promise ();
}
function Recycledata (i,length) {
$.when (GetData (i)). Done (function (data) {//Here is the data for defer stored in Ajax
if (i<length-1) {
recycledata (i+1,length);//Recursive
}}
)
}
</script>
Here we first create a Deffered object, save the Ajax returned data in the Ajax success function in the Deffered object, and then return the Deffered object. This ensures that this Ajax request completes the next time the AJAX request is made. The benefits of deferred objects include that it allows you to add multiple callback functions to an event's freedom. Or specify a callback function uniformly for multiple events.
Using jquery's deferred object to realize that all pictures in the page are loaded
If we load the picture, for the picture is loaded, we can usually use the listening image load method. Today, we mainly introduce the deferred object of jquery to judge.
The deferred object of jquery is the focus and difficulty of jquery. For functions that perform a long time, we usually use the deferred object.
About the deferred object, I'm here to introduce a little bit about $.when (). Then ()
function Successfunc () {Console.log ("success!");}
function Failurefunc () {Console.log ("failure!");}
$.when (
$.ajax ("/main.php"),
$.ajax ("/modules.php"),
$.ajax ("/lists.php")
). Then (Successfunc, FAILUREFUNC);
You can call multiple Ajax at the same time, and then return success or failure through then.
Or
$.when ($.ajax ("test1.html"), $.ajax ("test2.html")
. Done (function () {alert ("Haha, success!"). "); })
. Fail (function () {alert ("Error!) "); });
Let's get back to the point of using JQuery's deferred object to realize that all the pictures in the page load complete
var defereds = []; Defines an array that holds the deferred object
$imgs. each (function () {//imgs loops all pictures
var DFD = $. Deferred ()//Create a new Deferred object
$ (this). Load (Dfd.resolve ());//The execution status of the Deferred object is changed after the picture is loaded
Defereds.push (DFD );//push to Array
});
$.when.apply (null, defereds). Done (function () {
console.log (' Load compeleted ');
Because the parameters supported by $.when are $.when (dfd1, DFD2, Dfd3, ...), we use apply here to accept array parameters.
The above mentioned apply (), and extended to in JS, call () method and apply () method
I'm here to introduce the Apply ()
If we have the prints function:
function Prints (a,b,c,d) {
console.log (a+b+c+d);
}
function Example (a,b,c,d) {
prints.apply (this,[a,b,c,d]);
}
Example ("1", "SD", "Wq", "WQE")/output: 1SDWQWQE
Or we can write this:
Prints.apply (null,["foot", "Ben", "he", "home"]);/output: cloud-dwelling community