Angular2中怎么实现一个定时循环事件
1个回答
展开全部
如果真的要做push notification啥的,还是要从service端设计.private startTime: number = new Date().getTime();private theValue: number = 0;private FIVE_SECONDS: number = 5000;private getUpdate(): void {let currentTime = new Date().getTime();let diff = currentTime - this.startTime;if (diff / FIVE_SECONDS 0) {// Do somethingthis.theValue += 1;this.startTime = new Date().getTime();}}}我再看了一下问题, 貌似你是想要找个简单的function咯,可以这样:import { Observable } from 'rxjs/Rx';// .....export Class AppComponent {private theValue: number = 0;ngOnInit() {let timer = Observable.timer('some time', 2000);timer.subscribe(t = this.theValue += 1);}}这样就好啦,用一个Observable,然后subscribe,再在event里面作改变就好咯
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询