@@ -408,7 +408,6 @@ the specific language governing permissions and limitations under the Apache Lic
408408 */
409409 function ajax ( options ) {
410410 var timeout , // current scheduled but not yet executed request
411- requestSequence = 0 , // sequence used to drop out-of-order responses
412411 handler = null ,
413412 quietMillis = options . quietMillis || 100 ,
414413 ajaxUrl = options . url ,
@@ -417,9 +416,7 @@ the specific language governing permissions and limitations under the Apache Lic
417416 return function ( query ) {
418417 window . clearTimeout ( timeout ) ;
419418 timeout = window . setTimeout ( function ( ) {
420- requestSequence += 1 ; // increment the sequence
421- var requestNumber = requestSequence , // this request's sequence number
422- data = options . data , // ajax data function
419+ var data = options . data , // ajax data function
423420 url = ajaxUrl , // ajax url string or function
424421 transport = options . transport || $ . fn . select2 . ajaxDefaults . transport ,
425422 // deprecated - to be removed in 4.0 - use params instead
@@ -449,9 +446,6 @@ the specific language governing permissions and limitations under the Apache Lic
449446 dataType : options . dataType ,
450447 data : data ,
451448 success : function ( data ) {
452- if ( requestNumber < requestSequence ) {
453- return ;
454- }
455449 // TODO - replace query.page with query so users have access to term, page, etc.
456450 var results = options . results ( data , query . page ) ;
457451 query . callback ( results ) ;
@@ -703,6 +697,7 @@ the specific language governing permissions and limitations under the Apache Lic
703697 this . results = results = this . container . find ( resultsSelector ) ;
704698 this . search = search = this . container . find ( "input.select2-input" ) ;
705699
700+ this . queryCount = 0 ;
706701 this . resultsPage = 0 ;
707702 this . context = null ;
708703
@@ -1428,7 +1423,7 @@ the specific language governing permissions and limitations under the Apache Lic
14281423 if ( index >= choices . length ) index = choices . length - 1 ;
14291424 if ( index < 0 ) index = 0 ;
14301425
1431- this . results . find ( ".select2-highlighted" ) . removeClass ( "select2-highlighted" ) ;
1426+ this . removeHighlight ( ) ;
14321427
14331428 choice = $ ( choices [ index ] ) ;
14341429 choice . addClass ( "select2-highlighted" ) ;
@@ -1441,6 +1436,10 @@ the specific language governing permissions and limitations under the Apache Lic
14411436 }
14421437 } ,
14431438
1439+ removeHighlight : function ( ) {
1440+ this . results . find ( ".select2-highlighted" ) . removeClass ( "select2-highlighted" ) ;
1441+ } ,
1442+
14441443 // abstract
14451444 countSelectableResults : function ( ) {
14461445 return this . findHighlightableChoices ( ) . length ;
@@ -1453,8 +1452,8 @@ the specific language governing permissions and limitations under the Apache Lic
14531452 var choices = this . findHighlightableChoices ( ) ;
14541453 this . highlight ( choices . index ( el ) ) ;
14551454 } else if ( el . length == 0 ) {
1456- // if we are over an unselectable item remove al highlights
1457- this . results . find ( ".select2-highlighted" ) . removeClass ( "select2-highlighted" ) ;
1455+ // if we are over an unselectable item remove all highlights
1456+ this . removeHighlight ( ) ;
14581457 }
14591458 } ,
14601459
@@ -1521,7 +1520,9 @@ the specific language governing permissions and limitations under the Apache Lic
15211520 self = this ,
15221521 input ,
15231522 term = search . val ( ) ,
1524- lastTerm = $ . data ( this . container , "select2-last-term" ) ;
1523+ lastTerm = $ . data ( this . container , "select2-last-term" ) ,
1524+ // sequence number used to drop out-of-order responses
1525+ queryNumber ;
15251526
15261527 // prevent duplicate queries against the same term
15271528 if ( initial !== true && lastTerm && equal ( term , lastTerm ) ) return ;
@@ -1543,6 +1544,8 @@ the specific language governing permissions and limitations under the Apache Lic
15431544 postRender ( ) ;
15441545 }
15451546
1547+ queryNumber = ++ this . queryCount ;
1548+
15461549 var maxSelSize = this . getMaximumSelectionSize ( ) ;
15471550 if ( maxSelSize >= 1 ) {
15481551 data = this . data ( ) ;
@@ -1577,6 +1580,8 @@ the specific language governing permissions and limitations under the Apache Lic
15771580
15781581 search . addClass ( "select2-active" ) ;
15791582
1583+ this . removeHighlight ( ) ;
1584+
15801585 // give the tokenizer a chance to pre-process the input
15811586 input = this . tokenize ( ) ;
15821587 if ( input != undefined && input != null ) {
@@ -1594,6 +1599,11 @@ the specific language governing permissions and limitations under the Apache Lic
15941599 callback : this . bind ( function ( data ) {
15951600 var def ; // default choice
15961601
1602+ // ignore old responses
1603+ if ( queryNumber != this . queryCount ) {
1604+ return ;
1605+ }
1606+
15971607 // ignore a response if the select2 has been closed before it was received
15981608 if ( ! this . opened ( ) ) {
15991609 this . search . removeClass ( "select2-active" ) ;
0 commit comments