var list= data.data.list; List is an array of arrays returned by Ajax
if($("#li"+ list[0]. Id) .length>0) { return false; } //$ ("#learnList"). Remove (); //if the Li tag does not exist, follow the first-to-last addition to the Top-most Position. if(! ($ ("li"). Size ()> -)) { for(vari = list.length-1; I >=0;i--) { if(! ($ ("#li" + list[i]. Id). length>0)) { //If this li is not originally present, that is, the new learning situation varLi ="<li id= ' Li"+ list[i]. Id +"' >"+ list[i]. UserName +"classmate, in the ""+NewDate (parseint (list[i). Studytime.replace (/\d/igm,"")). tolocalestring () +"", Learn"+ list[i]. Segmentname +"</li>"; $ (li). prependto ($ ("#learnList") . Slidedown (); } Else { return false; } } } Else { for(vari =0; I < list.length;i++) { //for (var i = list.length-1; i >=0; i--) { if(! ($("#li"+ list[i]. Id). length >0)) { //If this li is not originally present, that is, the new learning situation varLi ="<li id= ' Li"+ list[i]. Id +"' >"+ list[i]. UserName +"classmate, in the ""+NewDate (parseint (list[i). Studytime.replace (/\d/igm,"")). tolocalestring () +"", Learn"+ list[i]. Segmentname +"</li>"; $ (li). prependto ($ ("#learnList") . Slidedown (); } Else { return false; } } }
The more important point is, (because the database is arranged in the order of time from near to far, so the first one is the nearest Data)
1.shtml page when the first load of data, should use the reverse i--, so that the first to get the data splicing of Li finally prependto to UL (of course, can also use i++, with Appendto can Also. )
2. Once again Ajax refresh, it is necessary to use I++,PREPENDTO to UL (of course, can also use i--, with Prependto can Also. )
In summary, if return False is the case, only 1.2. two steps are written in Reverse. If you change to continue, there is a little speed advantage in reverse writing, of course, it is negligible.
Optimize the code above and use Continue.
for(vari = list.length-1; I >=0; i--) { if(! ($("#li"+ list[i]. Id). length >0)) { //If this li is not originally present, that is, the new learning situation varLi ="<li id= ' Li"+ list[i]. Id +"' >"+ list[i]. UserName +"classmate, in the ""+NewDate (parseint (list[i). Studytime.replace (/\d/igm,"")). tolocalestring () +"", Learn"+ list[i]. Segmentname +"</li>"; $ (li). prependto ($ ("#learnList") . Slidedown (); } Else { //return false; Continue; //this is done in order to avoid first fetching the oldest data, encountered " If (! ( $ ("#li" + list[i]. Id). length > 0) ", The direct return false is Interrupted.
} }
3. Determine if the label and ID are not the same, one is size (), one is length
About Ajax pseudo Real-time dynamic Drop-down display up-to-date data