| 
									
										
										
										
											2016-02-02 14:39:34 +01:00
										 |  |  | // #docregion
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  | import { Component, OnDestroy, OnInit } from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-02-02 14:39:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							| 
									
										
										
										
											2016-05-20 19:07:01 -07:00
										 |  |  |   selector: 'countdown-timer', | 
					
						
							| 
									
										
										
										
											2016-02-02 14:39:34 +01:00
										 |  |  |   template: '<p>{{message}}</p>' | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2016-02-22 02:40:57 -08:00
										 |  |  | export class CountdownTimerComponent implements OnInit, OnDestroy { | 
					
						
							| 
									
										
										
										
											2016-02-02 14:39:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   intervalId = 0; | 
					
						
							|  |  |  |   message = ''; | 
					
						
							|  |  |  |   seconds = 11; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-22 02:40:57 -08:00
										 |  |  |   clearTimer() {clearInterval(this.intervalId);} | 
					
						
							| 
									
										
										
										
											2016-03-07 11:50:14 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-22 02:40:57 -08:00
										 |  |  |   ngOnInit()    { this.start(); } | 
					
						
							|  |  |  |   ngOnDestroy() { this.clearTimer(); } | 
					
						
							| 
									
										
										
										
											2016-03-07 11:50:14 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  |   start() { this.countDown(); } | 
					
						
							| 
									
										
										
										
											2016-02-22 02:40:57 -08:00
										 |  |  |   stop()  { | 
					
						
							|  |  |  |     this.clearTimer(); | 
					
						
							|  |  |  |     this.message = `Holding at T-${this.seconds} seconds`; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-03-07 11:50:14 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  |   private countDown() { | 
					
						
							| 
									
										
										
										
											2016-02-22 02:40:57 -08:00
										 |  |  |     this.clearTimer(); | 
					
						
							| 
									
										
										
										
											2016-05-20 19:07:01 -07:00
										 |  |  |     this.intervalId = window.setInterval(() => { | 
					
						
							| 
									
										
										
										
											2016-02-02 14:39:34 +01:00
										 |  |  |       this.seconds -= 1; | 
					
						
							| 
									
										
										
										
											2016-05-20 19:07:01 -07:00
										 |  |  |       if (this.seconds === 0) { | 
					
						
							|  |  |  |         this.message = 'Blast off!'; | 
					
						
							| 
									
										
										
										
											2016-02-02 14:39:34 +01:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2016-05-20 19:07:01 -07:00
										 |  |  |         if (this.seconds < 0) { this.seconds = 10; } // reset
 | 
					
						
							| 
									
										
										
										
											2016-02-02 14:39:34 +01:00
										 |  |  |         this.message = `T-${this.seconds} seconds and counting`; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, 1000); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  | } |