I have a piece of code as follows. It reacts to the change of the input field by running treat
:
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
</head>
<body ng-controller="Ctrl">
<input type="text" ng-model="text"></input>
<div id="console"></div>
<script>
var app = angular.module('app', []);
app.controller('Ctrl', ["$scope", function ($scope) {
$scope.$watch('text', function (newValue, oldValue) {
treat(newValue);
}, false)
}])
function treat(x) {
debug(x)
}
function debug(msg) {
document.getElementById("console").innerHTML += msg + "<br/>";
}
</script>
</body>
</html>
Now, I want to do something special: I want to set a minimal interval (say 500 ms
) between 2 treat
. So if another treat
is supposed to run very shortly after a treat
, I want it to wait a little bit; if the time interval between 2 treat
is larger than 500 ms
, they could run one after another as normal (I don't want to add a 500 ms
on top of that).
Does anyone know how to achieve this?