Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Service 角度2-停止/取消/清除路线中可观察到()_Service_Angular_Observable - Fatal编程技术网

Service 角度2-停止/取消/清除路线中可观察到()

Service 角度2-停止/取消/清除路线中可观察到(),service,angular,observable,Service,Angular,Observable,我对Angular2不熟悉,所以如果这是一个微不足道的问题,我深表歉意。当我点击另一个链接时,我似乎无法停止间隔。该组件每3,5秒从DB表中获取一次数据,并将其用作聊天室,因此100个用户可以随时添加到该组件中。我不想让它一直在后台运行,所以我想我会使用routeronDeactivate()函数,当用户在不同的链接上时,使其停止 我想我做错了什么。有人能帮我吗 export class FeedComponent { public feeditems: Feed[]; pub

我对Angular2不熟悉,所以如果这是一个微不足道的问题,我深表歉意。当我点击另一个链接时,我似乎无法停止间隔。该组件每3,5秒从DB表中获取一次数据,并将其用作聊天室,因此100个用户可以随时添加到该组件中。我不想让它一直在后台运行,所以我想我会使用
routeronDeactivate()
函数,当用户在不同的链接上时,使其停止

我想我做错了什么。有人能帮我吗

export class FeedComponent {

    public feeditems: Feed[];
    public timer;


    constructor(private _feedService: FeedService) {
    }

    getArticlesJSON() {

        console.log('getArticlesJSON');
        this._feedService.getFeedJSON()
            .subscribe(
                data => this.feeditems = data,
                err => console.log(err),
                () => console.log('Completed')
            );
    }

    routerOnDeactivate() {

        // this.timer.remove();
        // this.timer.dematerialize();
        // clearInterval(this.timer);

        console.log('-- deactivate ');
        // console.log('-- deactivate ' + JSON.stringify(this.timer));
    }

    routerOnActivate() {

        this.timer = Observable.timer(5000,3500);
        this.timer.subscribe(() => {
            this.getArticlesJSON();
        });

        console.log('++ activate ' + JSON.stringify(this.timer));
    }
}
routerOnActivate(){
this.timer=可观察的计时器(50003500);
this.subscription=this.timer.subscription(()=>{
这个.getArticlesJSON();
});
}
路由器(){
this.subscription.unsubscripte();
}

这是否意味着取消订阅将计时器重置为零?我不知道“重置为零”是什么意思。这意味着可观察对象不再调用
()=>{this.getArticlesJSON();}
。在通常情况下,当我们取消订阅时,它会将计时器变为零或null?需要在之后重置计时器计数器吗?否。
this.timer
只是一个
订阅
参考,允许您取消订阅。如果你想要另一个计数器,你必须再次调用
Observable.timer(…)
,这个计数器开始计数,与第一次计数相同。那么我们如何暂停并恢复它?