1

I'm using a Jquery plugin in my angular2 project. It has a serious performance problem if the plugin has an event like $(window).mousemove() or setInterval(function(){}, 10). Because zone.js has hooks to html events, the page always checks data changes. My cpu is running very high.

2

2 Answers 2

1

You can use NgZone to make code run inside or outside of Angulars zone.

Outside for performance reasons, inside to ensure Angular gets notified that change detection has to be run when properties of a component, model, or service are modiefied:

constructor(private zone:NgZone) {
  zone.runOutsideAngular(() => {
    $(window).mousemove()...;
    setInterval(() => {}, 10);
  })
}

onMouseMove() {
  this.zone.run(() => {
    this.someProp = 'someValue';
    this.router.navigate(....)
  });
}
Sign up to request clarification or add additional context in comments.

9 Comments

ngZone.runOutsideAngular is working doCheck call once. but if the route jumps doCheck always call.
What do you mean by "if the route jumps doCheck"?
I don't get how routing is related to your question.
from router '/a' to router '/b' runOutsideAngular is not work. refresh '/b' runOutsideAngular is work
That's why I added ` this.router.navigate(....)` as example inside this.zone.run(). router.navigate() depends on change detection to happen. With runOutsideAngular() updating the view won't work.
|
1

you can use

@HostListener('mousemove', ['$event'])
  onMousemove(event: MouseEvent) { this.mousemove.emit(event); } 

instead $(window).mousemove()

will increase your performance

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.