I'm trying to call a function after a short delay after the user stops typing, but clearTimeout()doesn't seem to do what I think. Inside the Angular JS controller is the following.
$scope.typing = false;
$scope.delay = undefined;
$scope.startTyping = function() {
$scope.typing = true;
console.log('start1', $scope.delay);
clearTimeout( $scope.delay );
console.log('start2', $scope.delay);
}
$scope.stopTyping = function() {
$scope.typing = false;
$scope.delay = setTimeout( $scope.lookup, 1000);
}
$scope.lookup = function() {
if ($scope.typing === true){
return false;
}
console.log('lookup');
I see lookupin the logs for each key, and not after each delay. Why is this?
Update
After registering the value, delayit is clear that it clearTimeout()does not reset the timer, and instead, several timers are set, and each of them starts the search function.
For reference...
For anyone else troubleshooting clearTimeout(), here are some similar questions that might solve your problem (but not mine):
clearTimeout not working
clearTimeout () does not work
clearTimeout not working
clearTimeout not working