title: Javascript: unable to clearTimeout
body: <p>I'm trying to call a function after a short delay after a user stops typing, but <code>clearTimeout()</code> does not seem to be doing what I think. The following is inside an Angular JS controller.</p>
  
  <pre><code>$scope.typing = false;
  $scope.delay = undefined;
  
  //Triggered by ng-keydown...
  $scope.startTyping = function() {
      $scope.typing = true;
              console.log('start1', $scope.delay); //6
      clearTimeout( $scope.delay );
              console.log('start2', $scope.delay); //6... NOT getting cleared!
  }
  
  //Triggered by ng-keyup...
  $scope.stopTyping = function() {
      $scope.typing = false;
      $scope.delay = setTimeout( $scope.lookup, 1000);
  }
  
  $scope.lookup = function() {
  
      if ($scope.typing === true){
          return false;
      }
      console.log('lookup'); //This fires after every key!
  </code></pre>
  
  <p>I see <code>lookup</code> in the logs for every key, instead of after every delay. Why is this?</p>
  
  <p><strong>Update</strong></p>
  
  <p>After logging the value of <code>delay</code> it is clear that <code>clearTimeout()</code> is not reseting the timer and instead, multiple timers are getting set and each one is triggering the lookup function.</p>
  
  <p><em>For reference...</em></p>
  
  <p>For anyone else troubleshooting <code>clearTimeout()</code>, here are some similar questions that may solve your problem (but didn't solve mine):</p>
  
  <p><a href="https://stackoverflow.com/questions/5462948/cleartimeout-not-working">clearTimeout not working</a></p>
  
  <p><a href="https://stackoverflow.com/questions/7173419/cleartimeout-not-working">clearTimeout() not working</a></p>
  
  <p><a href="https://stackoverflow.com/questions/6549298/cleartimeout-is-not-working">clearTimeout is not working</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5397117/cleartimeout-not-working">clearTimeout not working</a></p>