About Ajax pseudo Real-time dynamic Drop-down display up-to-date data

Source: Internet
Author: User

 
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

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.