Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
14
rated 0 times [  15] [ 1]  / answers: 1 / hits: 22405  / 11 Years ago, sat, march 30, 2013, 12:00:00

Can't seem to google up an example of how this is done.



I have successfully created a textbox that calls a function every time it changes. What I would like to do is only call the function when the user has stopped typing for x milliseconds.



I know how to do it in JQuery using the keyup event, and can probably make it work this way, but want to do it the Angular Way.



Edit



Maybe it wasn't clear from the tag or text, but I am using AngularJS, and want to use correct methodology for that framework to create this delay functionality.


More From » angularjs

 Answers
31

For angular approach can inject $timeout in controller as dependency and use $watch on scope variable you've assigned in ng-model.



var timer=false;
$scope.ModelName='foo';
$scope.$watch('ModelName', function(){
if(timer){
$timeout.cancel(timer)
}
timer= $timeout(function(){
/* run code*/
},delay)
});

[#79221] Friday, March 29, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mitchell

Total Points: 95
Total Questions: 110
Total Answers: 87

Location: Gabon
Member since Thu, Jul 15, 2021
3 Years ago
;