Skip to content

Commit dd3265d

Browse files
authored
fix(module:carousel): fix carousel autoplay bug (#1309)
close #1242
1 parent e63668d commit dd3265d

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

Diff for: components/carousel/nz-carousel.component.ts

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
import {
2+
AfterContentInit,
23
AfterViewInit,
34
Component,
45
ContentChildren,
56
ElementRef,
67
EventEmitter,
78
HostBinding,
89
Input,
9-
NgZone,
1010
OnDestroy,
1111
Output,
1212
QueryList,
1313
Renderer2,
1414
ViewChild
1515
} from '@angular/core';
1616
import { Subscription } from 'rxjs/Subscription';
17-
import { first } from 'rxjs/operators/first';
18-
1917
import { toBoolean } from '../core/util/convert';
2018

2119
import { NzCarouselContentDirective } from './nz-carousel-content.directive';
@@ -67,15 +65,15 @@ import { NzCarouselContentDirective } from './nz-carousel-content.directive';
6765
`
6866
]
6967
})
70-
export class NzCarouselComponent implements AfterViewInit, OnDestroy {
68+
export class NzCarouselComponent implements AfterViewInit, OnDestroy, AfterContentInit {
7169
private _autoPlay = false;
7270
private _dots = true;
7371
private _vertical = false;
7472
private _effect = 'scrollx';
7573
slideContentsSubscription: Subscription;
7674
activeIndex = 0;
7775
transform = 'translate3d(0px, 0px, 0px)';
78-
interval;
76+
timeout;
7977

8078
@ContentChildren(NzCarouselContentDirective) slideContents: QueryList<NzCarouselContentDirective>;
8179
@ViewChild('slickList') slickList: ElementRef;
@@ -153,7 +151,6 @@ export class NzCarouselComponent implements AfterViewInit, OnDestroy {
153151

154152
renderContent(): void {
155153
if (this.slideContents && this.slideContents.length) {
156-
this.slideContents.first.isActive = true;
157154
this.slideContents.forEach((content, i) => {
158155
content.width = this.elementRef.nativeElement.offsetWidth;
159156
if (this.nzEffect === 'fade') {
@@ -187,9 +184,9 @@ export class NzCarouselComponent implements AfterViewInit, OnDestroy {
187184
}
188185

189186
setUpAutoPlay(): void {
190-
this.clearInterval();
187+
this.clearTimeout();
191188
if (this.nzAutoPlay) {
192-
this.interval = setInterval(_ => {
189+
this.timeout = setTimeout(_ => {
193190
this.setActive(this.slideContents.toArray()[ this.nextIndex ], this.nextIndex);
194191
}, 3000);
195192
}
@@ -202,10 +199,10 @@ export class NzCarouselComponent implements AfterViewInit, OnDestroy {
202199
}
203200
}
204201

205-
clearInterval(): void {
206-
if (this.interval) {
207-
clearInterval(this.interval);
208-
this.interval = null;
202+
clearTimeout(): void {
203+
if (this.timeout) {
204+
clearTimeout(this.timeout);
205+
this.timeout = null;
209206
}
210207
}
211208

@@ -233,24 +230,28 @@ export class NzCarouselComponent implements AfterViewInit, OnDestroy {
233230
}
234231
}
235232

236-
constructor(public elementRef: ElementRef, private renderer: Renderer2, private zone: NgZone) {
233+
constructor(public elementRef: ElementRef, private renderer: Renderer2) {
234+
}
235+
236+
ngAfterContentInit(): void {
237+
if (this.slideContents && this.slideContents.length) {
238+
this.slideContents.first.isActive = true;
239+
}
237240
}
238241

239242
ngAfterViewInit(): void {
240243
this.slideContentsSubscription = this.slideContents.changes.subscribe(() => {
241244
this.renderContent();
242245
});
243-
this.zone.onStable.pipe(first()).subscribe(() => {
244-
this.renderContent();
245-
});
246+
this.renderContent();
246247
}
247248

248249
ngOnDestroy(): void {
249250
if (this.slideContentsSubscription) {
250251
this.slideContentsSubscription.unsubscribe();
251252
this.slideContentsSubscription = null;
252253
}
253-
this.clearInterval();
254+
this.clearTimeout();
254255
}
255256

256257
}

Diff for: components/carousel/nz-carousel.spec.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, ViewChild } from '@angular/core';
2-
import { discardPeriodicTasks, fakeAsync, tick, TestBed } from '@angular/core/testing';
2+
import { fakeAsync, tick, TestBed } from '@angular/core/testing';
33
import { By } from '@angular/platform-browser';
44

55
import { dispatchKeyboardEvent } from '../core/testing';
@@ -109,21 +109,19 @@ describe('carousel', () => {
109109
fixture.detectChanges();
110110
expect(carouselWrapper.nativeElement.querySelector('.slick-track').style.transform).not.toBe('translate3d(0px, 0px, 0px)');
111111
});
112-
it('should autoplay work', fakeAsync((done) => {
112+
it('should autoplay work', fakeAsync(() => {
113+
testComponent.autoPlay = true;
113114
fixture.detectChanges();
114115
expect(carouselContents[ 0 ].nativeElement.classList).toContain('slick-active');
115-
testComponent.autoPlay = true;
116116
fixture.detectChanges();
117117
tick(3000 + 10);
118118
fixture.detectChanges();
119-
discardPeriodicTasks();
120119
expect(carouselContents[ 1 ].nativeElement.classList).toContain('slick-active');
121120
carouselWrapper.nativeElement.querySelector('.slick-dots').lastElementChild.click();
122-
testComponent.autoPlay = true;
123121
fixture.detectChanges();
124122
tick(3000 + 10);
125123
fixture.detectChanges();
126-
discardPeriodicTasks();
124+
testComponent.nzCarouselComponent.clearTimeout();
127125
expect(carouselContents[ 0 ].nativeElement.classList).toContain('slick-active');
128126
}));
129127
it('should func work', () => {

0 commit comments

Comments
 (0)