1

I have a kludge in angular application that checks dom tree regularly (using javascript function setinterval) and updates some stuff (this code is related to lack of some DOM tree events). It will take me a long time to move this functinality into angular-approved way and I'm not very fond of this migration idea: "don't touch it if it works" rule :) There is only one problem - it looks like setinterval forces whole angular application to check for changes. How can I suppress this issue? I want to disable change detection for this only timer.

Thank you in advance!

2 Answers 2

2

I will recommend you to use the ON Push Change detection strategy.

 changeDetection: ChangeDetectionStrategy.OnPush

The following actions do not trigger change detection using the OnPush change detection strategy:

setTimeout, setInterval, Promise.resolve().then(), (of course, the same for Promise.reject().then()) this.http.get('...').subscribe() (in general, any RxJS observable subscription)

SO If you want to run change detection for a case or for other places you can use: ChangeDetectorRef package.

import { ChangeDetectionStrategy, Component, ChangeDetectorRef  } 

constructor(private c: ChangeDetectorRef ) {}



 this.c.detectChanges()  // call where you need it
Sign up to request clarification or add additional context in comments.

Comments

1

NgZone API allows us to execute certain code outside Angular’s Zone.

Inject NgZone service then use runOutsideAngular method to run the certain code outside angular zone.

constructor(private ngZone: NgZone){}

this.ngZone.runOutsideAngular(()=>{
      setInterval(()=>{
       .....
      })
 });

1 Comment

Thank you, sir! That's exactly what I was looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.