मैंने 10 सेकंड के बाद अपलोड पेज पर जाने के लिए सेटटाइमआउट का इस्तेमाल किया। लेकिन टाइमआउट तब जारी रहता है जब उपयोगकर्ता दूसरे पेज पर नेविगेट करता है। मैंने इस लिंक के निर्देशों का पालन किया (Angular 2 setinterval() चालू रहता है अन्य घटक) और ngOnDestroy में clearTimeout जोड़ा, लेकिन यह काम नहीं करता है।
ngOnInit() {
// Get uploaded file info
this.uploadService.getFileInfo().subscribe((value) => {
this.fileInfo = value;
this.http.post(environment.apiUrl+'/api/dataValidation/validateData.php', this.fileInfo)
.subscribe(res => {
console.log('response'+res)
if (res['length']<5) {
//console.log(res);
this.failure = true;
this.fileValidationResults.errorMessage =res[0];
this.loading = false; // stop loading screen
this._timer =setTimeout(() => {
this.router.navigate(["/uploads/upload"]);
}, 10000);
}
})
});
}
ngOnDestroy() {
if (this._timer){
clearTimeout(this._timer);
};
}
जब उपयोगकर्ता किसी अन्य लिंक पर नेविगेट करता है और सेटटाइमआउट() केवल अपने स्वयं के घटक में काम करता है तो मैं सेटटाइमआउट() को कैसे रोक सकता हूं?
1 उत्तर
चाहिए काम करना चाहिए, याद रखें कि कब अपने घटक को OnDestroy लागू करने की घोषणा करें
BTW, आप सेटटाइमआउट के बजाय rxjs के timer
ऑपरेटर का उपयोग कर सकते हैं
import {timer} from 'rxjs'
import {takeWhile} from 'rxjs/operators'
...
export class OneComponent implements OnInit,OnDestroy {
alive:boolean=true;
ngOnInit(){
timer(1000).pipe(takeWhile(()=>this.alive)).subscribe(_=>{
this.router.navigate(['/three'])
})
}
ngOnDestroy()
{
this.alive=false;
}
}
संबंधित सवाल
जुड़े हुए प्रश्न
नए सवाल
angular
Google से वेब फ्रेमवर्क के बारे में प्रश्न इस टैग का उपयोग कोणीय प्रश्नों के लिए करें जो एक व्यक्तिगत संस्करण के लिए विशिष्ट नहीं हैं। पुराने AngularJS (1.x) वेब ढांचे के लिए, कोणीयज टैग का उपयोग करें।