Guest User

Untitled

a guest
Dec 14th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. private _timeoutSeconds: number = 15;
  2. private timerSubscription: Subscription;
  3. private timer: Observable<number>;
  4. private resetOnTrigger: boolean = false;
  5. public timeoutExpired: Subject<number> = new Subject<number>();
  6.  
  7.  
  8.  
  9. constructor(
  10. public router: Router,
  11. private cd: ChangeDetectorRef) {
  12.  
  13. this.timeoutExpired.subscribe(n => {
  14. alert("logout");
  15. });
  16.  
  17. this.startTimer();
  18.  
  19. }
  20.  
  21. public startTimer() {
  22. if (this.timerSubscription) {
  23. this.timerSubscription.unsubscribe();
  24. }
  25. this.timer = Observable.timer(this._timeoutSeconds * 1000);
  26. this.timerSubscription = this.timer.subscribe(n => {
  27. this.timerComplete(n);
  28. });
  29. }
  30.  
  31. public stopTimer() {
  32. console.log(this.cd + " " + "stop timer");
  33. this.timerSubscription.unsubscribe();
  34. }
  35.  
  36. public resetTimer() {
  37. console.log(this.cd + " " + "reset timer");
  38. if (this.timerSubscription) {
  39. this.timerSubscription.unsubscribe();
  40. }
  41.  
  42. this.timer = Observable.timer(this._timeoutSeconds * 1000);
  43. this.timerSubscription = this.timer.subscribe(n => {
  44. this.timerComplete(n);
  45. });
  46. }
  47.  
  48. private timerComplete(n: number) {
  49. console.log(this.cd.detectChanges() + " " + "timer complete");
  50. this.timeoutExpired.next(++this._count);
  51. this.resetTimer();
  52. if (this.resetOnTrigger) {
  53. this.startTimer();
  54. }
  55. }
Add Comment
Please, Sign In to add comment