Let's assume I have some inputs with their ng-models
<input type="text" ng-model="vm.man.name">
<input type="text" ng-model="vm.man.surName">
In my controller I have object vm.man={};
.
I want to do the following:
I want to put $watch on my vm.man
,
so if I press some key inside my <input>
, it should wait 3 seconds and then send response to the server. However, if I pressed some key, and during 3 seconds I pressed another key it should wait 3 seconds and then respond to the server. Here is what I have already done:
$scope.$watch(()=>vm.man, function (newVal, oldVal) {
if(angular.isUndefined(newVal)) {
return console.log("newVal is Undefined")
}else{
vm.timer1= $timeout(function () {
vm.someFunctionThatRespondsToServer();
}, 3000);
}
}, true);
The problem I have now, it creates response per keypress. for example I pressed a
in my input
it responds a, then I pressed b
it responds ab
and etc.
I want to get something like this: I pressed a
it waits 3 seconds if there is no other keypresses, it responds, or I pressed a
and then after 2 seconds, lets say I pressed b
it waits 3 seconds and then responds ab
if there were no other keypresses during 3seconds.
Thanks in advance.