0
こんにちは、タイマーを使用してサブスクリプションを使用して時間枠内で関数を実行する方法をテストしています。私が抱えている問題は、一旦私が加入解除し、コンポーネントを変更すると、メソッドは呼び出されるまでです。私はそれを止める方法を知りたいです。これは私のコードです:Angular 2 MVCサブスクライブ後にタイマーを呼び出すメソッドをサブスクライブした後に
`
import { Component, OnInit, AfterViewChecked, AfterContentInit, AfterViewInit, OnDestroy } from '@angular/core';
import $ from 'jquery';
import { Headers, Http } from "@angular/http";
import "rxjs/Rx";
import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';
// Variables that load the respectively JavasScript methods in the JavaScript file SharedContents/plugins/flot-charts/jquery.flot.js
declare var initRealTimeChart: any;
declare var initSparkline: any;
declare var initDonutChart: any;
@Component({
selector: 'home',
template: require('./home.component.html')
})
export class HomeComponent implements OnInit, AfterViewChecked, AfterContentInit, AfterViewInit, OnDestroy {
_message: string;
foo;
private subscriptions: Array<Subscription> = [];
//called first time before the ngOnInit()
constructor(private http: Http) {}
//called after the constructor and called after the first ngOnChanges()
ngOnInit(): void {
this._message = "adsad";
console.log(this._message);
//setInterval(() => this.getLog("Home/GetTest"), 500);
this.subscriptions.push(this.getLog("Home/GetTest").subscribe(
response => {
console.log('Success ' + response);
},
err => {
console.log('Error' + err)
},
() => {
console.log('Complete')
}
));
}
ngAfterContentInit(): void {
}
ngAfterViewChecked(): void {
}
ngAfterViewInit(): void {
initRealTimeChart();
initSparkline();
initDonutChart();
}
ngOnDestroy(): void {
console.log("ngOnDestroy!");
this.subscriptions.forEach((subscription: Subscription) => {
console.log("Destroying Subscription :", subscription);
subscription.unsubscribe();
});
}
public TestStuff() {
console.log("Run TestTSuff");
return this.http.get("Home/GetTest")
.toPromise()
.then(response => {
response.json().data;
console.log('GOT IT!');
console.log(response);
})
.catch((ex) => {
console.log(ex);
console.log(ex._body);
});
}
getLog(url: string): Observable<string> {
return Observable
.timer(0, 12000)
.concatMap(() => this.get(url))
///.retryWhen(error => error.delay(12000))
.map((data: any) => {
console.log('Request Success!')
data.json();
return data.json();
})
.catch(e => {
//console.warn(e.toString());
return Observable.from("ERROR" + e);
});
}
get(url) {
return this.http.get(url);
}
}
`