I'm building a angular directive, which on input value change runs a function, i want to wait 300ms before running this function, but if the value changes before the 300ms has gone, i need to reset the delay to 300ms. So only if value changes for 300ms then the function should run:
My code
(function() {
'use strict';
angular
.module('address',[])
.directive('address', ['Address', function (Address) {
return {
restrict: 'E',
transclude: true,
templateUrl: 'partials/address.html',
link: function (scope, elem, attrs) {
var delay = "300";
scope.$watch('searchparam', function(){
if(!_.isUndefined(scope.searchparam) && scope.searchparam.length >= 3) {
// here i need some delay timer
Address.get(scope.searchparam).then(function(data){
console.log("data: ", data);
scope.addressList = data;
}, function(status){
console.log("status: ", status);
scope.errorHandler = status;
});
}
});
}
};
}]);
})();
The address.get is an async service that returns addresses