@@ -29,7 +29,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
2929 } ;
3030} ] )
3131
32- . directive ( 'typeahead' , [ '$compile' , '$parse' , '$q' , '$document' , '$position' , 'typeaheadParser' , function ( $compile , $parse , $q , $document , $position , typeaheadParser ) {
32+ . directive ( 'typeahead' , [ '$compile' , '$parse' , '$q' , '$timeout' , '$ document', '$position' , 'typeaheadParser' , function ( $compile , $parse , $q , $timeout , $document , $position , typeaheadParser ) {
3333
3434 var HOT_KEYS = [ 9 , 13 , 27 , 38 , 40 ] ;
3535
@@ -42,6 +42,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
4242 //minimal no of characters that needs to be entered before typeahead kicks-in
4343 var minSearch = originalScope . $eval ( attrs . typeaheadMinLength ) || 1 ;
4444
45+ //minimal wait time after last character typed before typehead kicks-in
46+ var waitTime = originalScope . $eval ( attrs . typeaheadWaitMs ) || 0 ;
47+
4548 //expressions used by typeahead
4649 var parserResult = typeaheadParser . parse ( attrs . typeahead ) ;
4750
@@ -124,12 +127,23 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
124127 //$parsers kick-in on all the changes coming from the view as well as manually triggered by $setViewValue
125128 modelCtrl . $parsers . push ( function ( inputValue ) {
126129
130+ var timeoutId ;
131+
127132 resetMatches ( ) ;
128133 if ( selected ) {
129134 return inputValue ;
130135 } else {
131136 if ( inputValue && inputValue . length >= minSearch ) {
132- getMatchesAsync ( inputValue ) ;
137+ if ( waitTime > 0 ) {
138+ if ( timeoutId ) {
139+ $timeout . cancel ( timeoutId ) ; //cancel previous timeout
140+ }
141+ timeoutId = $timeout ( function ( ) {
142+ getMatchesAsync ( inputValue ) ;
143+ } , waitTime ) ;
144+ } else {
145+ getMatchesAsync ( inputValue ) ;
146+ }
133147 }
134148 }
135149
0 commit comments