Jason Tree Jason Tree - 1 month ago 13
AngularJS Question

How do you reset a “$timeout”, and disable a “$watch()”?

How do you reset a “$timeout”, and disable a “$watch()”?

var customTimeout = $timeout(function () {
// arbitrary code
}, 55);


The key to both is assigning the result of the function to a variable.

To cleanup the timeout, just “.cancel()” it:


The same applies to “$interval()”.

To disable a watch, just call it.

.$watch() returns a deregistration function that we store to a variable

var deregisterWatchFn = $rootScope.$watch(‘someGloballyAvailableProperty’, function (newVal) {
  if (newVal) {
    // we invoke that deregistration function, to disable the watch