Angular.JS Quiz
Answer:
The key to both is assigning the result of the function to a variable.
To cleanup the timeout, just “.cancel()” it:
1 2 3 4 5 |
[crayon-676c0b8ec617d474508086 inline="true" class="language-js hljs"]<span class="hljs-keyword">var</span> customTimeout = $timeout(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">()</span> {</span> <span class="hljs-comment">// arbitrary code</span> }, <span class="hljs-number">55</span>); $timeout.cancel(customTimeout); |
[/crayon] The same applies to “$interval()”.
To disable a watch, just call it.
1 2 3 4 5 6 7 8 |
[crayon-676c0b8ec6180926290347 inline="true" ]// .$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 deregisterWatchFn(); ... } }); |
[/crayon]
If you want to explore more, visit our Angular.JS edu & tutorials section! Below are some examples:
Let’s build a webstore in Angular.JS
Angular.JS Project Management