Skip to content

Commit

Permalink
Merge pull request #8 from gimox/master
Browse files Browse the repository at this point in the history
Removing listeners on destroy to fix browser crash
  • Loading branch information
sheikalthaf authored Jan 21, 2018
2 parents 363e341 + c239b63 commit c5d83a9
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions src/ngu-carousel/ngu-carousel.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ export class NguCarouselComponent
isLast: false,
RTL: false
};

public listener1: any;
public listener2: any;
public listener3: any;
public listener4: any;
public listener5: any;
public listener6: any;
public listener7: any;
public listener8: any;

constructor(
private el: ElementRef,
Expand Down Expand Up @@ -210,10 +219,10 @@ export class NguCarouselComponent
}

ngAfterContentInit() {
this.renderer.listen(this.leftBtn, 'click', () =>
this.listener1 = this.renderer.listen(this.leftBtn, 'click', () =>
this.carouselScrollOne(0)
);
this.renderer.listen(this.rightBtn, 'click', () =>
this.listener2 = this.renderer.listen(this.rightBtn, 'click', () =>
this.carouselScrollOne(1)
);

Expand All @@ -226,7 +235,7 @@ export class NguCarouselComponent
this.carouselInterval();
this.onWindowScrolling();
this.touch();
this.renderer.listen('window', 'resize', event => {
this.listener3 = this.renderer.listen('window', 'resize', event => {
this.onResizing(event);
});
}
Expand Down Expand Up @@ -260,6 +269,29 @@ export class NguCarouselComponent
clearInterval(this.carouselInt);
// tslint:disable-next-line:no-unused-expression
this.itemsSubscribe && this.itemsSubscribe.unsubscribe();

// remove listeners
this.listener1();
this.listener2();
if(this.listener3) {
this.listener3();
}
if(this.listener4) {
this.listener4();
}
if(this.listener5) {
this.listener5();
}
if(this.listener6) {
this.listener6();
}
if(this.listener7) {
this.listener7();
}
if(this.listener8) {
this.listener8();
}

}

private onResizing(event: any) {
Expand Down Expand Up @@ -675,23 +707,23 @@ export class NguCarouselComponent
/* handle the auto slide */
private carouselInterval(): void {
if (typeof this.userData.interval === 'number' && this.data.loop) {
this.renderer.listen(this.carouselMain, 'touchstart', () => {
this.listener4 = this.renderer.listen(this.carouselMain, 'touchstart', () => {
this.carouselIntervalEvent(1);
});

this.renderer.listen(this.carouselMain, 'touchend', () => {
this.listener5 = this.renderer.listen(this.carouselMain, 'touchend', () => {
this.carouselIntervalEvent(0);
});

this.renderer.listen(this.carouselMain, 'mouseenter', () => {
this.listener6 = this.renderer.listen(this.carouselMain, 'mouseenter', () => {
this.carouselIntervalEvent(1);
});

this.renderer.listen(this.carouselMain, 'mouseleave', () => {
this.listener7 = this.renderer.listen(this.carouselMain, 'mouseleave', () => {
this.carouselIntervalEvent(0);
});

this.renderer.listen('window', 'scroll', () => {
this.listener8 = this.renderer.listen('window', 'scroll', () => {
clearTimeout(this.onScrolling);
this.onScrolling = setTimeout(() => {
this.onWindowScrolling();
Expand Down

0 comments on commit c5d83a9

Please sign in to comment.