I am in the process of creating an angular directive that will run a function when the input value changes. I want to implement a 300ms delay before running this function, but if the value changes again within the 300ms timeframe, I need to reset the delay back to 300ms. Therefore, the function should only be executed after the value has remained unchanged for 300ms:
Here is my code snippet
(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) {
// Implementing a delay timer here
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 function is an asynchronous service that returns addresses.