Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export class LS {
  2.  
  3.     public set (key: string, data: any) {
  4.         let result = (data.constructor == Object) ? JSON.stringify(data) : data;
  5.         localStorage.setItem(key, result);
  6.     }
  7.  
  8.     public get (key: string) : any {
  9.         let jsonObject = null;
  10.         let data = localStorage.getItem(key);
  11.        
  12.         try {
  13.             jsonObject = JSON.parse(data);
  14.         }
  15.         catch(e) {
  16.             jsonObject = data;
  17.         }
  18.        
  19.         return jsonObject;
  20.     }
  21.    
  22.     public rm(key:string) {
  23.         localStorage.removeItem(key);
  24.     }
  25.  
  26. }
  27.  
  28. export class CountdownService {
  29.  
  30.     protected timeData: Object = {
  31.         days: 0,
  32.         hours: 0,
  33.         minutes: 0,
  34.         seconds: 0
  35.     };
  36.  
  37.     public ONE_SECONDS = 1000;
  38.  
  39.     protected callback:Function;
  40.     protected ls:LS;
  41.     protected nameTimeData:string = 'timingData';
  42.  
  43.     constructor() {
  44.         this.ls = new LS();
  45.     }
  46.  
  47.     public start(hours: number, minutes:number, _callback:Function) {
  48.         this.callback = _callback;
  49.         let milliseconds = this.toMilliseconds(hours, minutes);
  50.         let deadline = new Date(Date.parse(new Date().toString()) + milliseconds);
  51.  
  52.         this.initializeClock(deadline);
  53.     }
  54.    
  55.     public getTimeData():Object {
  56.         return this.timeData
  57.     }
  58.  
  59.     protected toMilliseconds(hours, minutes) {
  60.         let secondsHours = hours * 3600;
  61.         let secondsMinutes = minutes * 60;
  62.         return (secondsHours + secondsMinutes) * this.ONE_SECONDS;
  63.     }
  64.  
  65.     protected getTimeRemaining(endTime) {
  66.  
  67.         let currentTime = new Date().toString();
  68.         /*let lsTime;
  69.  
  70.         // This block does not work correctly
  71.         if (this.ls.get(this.nameTimeData) != null) {
  72.             lsTime = this.ls.get(this.nameTimeData);
  73.             console.log(lsTime);
  74.             this.ls.set(this.nameTimeData, new Date().toString());
  75.         } else {
  76.             this.ls.set(this.nameTimeData, new Date().toString());
  77.             lsTime = this.ls.get(this.nameTimeData);
  78.         }
  79. */
  80.         let t = Date.parse(endTime) - Date.parse( currentTime );
  81.         let seconds = Math.floor((t / this.ONE_SECONDS) % 60);
  82.         let minutes = Math.floor((t / this.ONE_SECONDS / 60) % 60);
  83.         let hours = Math.floor((t / (this.ONE_SECONDS * 60 * 60)) % 24);
  84.         let days = Math.floor(t / (this.ONE_SECONDS * 60 * 60 * 24));
  85.  
  86.         return {
  87.             'total': t,
  88.             'days': days,
  89.             'hours': hours,
  90.             'minutes': minutes,
  91.             'seconds': seconds
  92.         };
  93.     }
  94.  
  95.     protected initializeClock(endTime) {
  96.  
  97.         let updateClock = () => {
  98.             let t  = this.getTimeRemaining(endTime);
  99.  
  100.             this.timeData['days'] = t['days'];
  101.             this.timeData['hours'] = ('0' + t.hours).slice(-2);
  102.             this.timeData['minutes'] = ('0' + t.minutes).slice(-2);
  103.             this.timeData['seconds'] = ('0' + t.seconds).slice(-2);
  104.  
  105.             if (t.total <= 0) {
  106.                 clearInterval(timeInterval);
  107.                 this.callback();
  108.             }
  109.         };
  110.  
  111.         updateClock();
  112.         var timeInterval = setInterval(updateClock, this.ONE_SECONDS);
  113.     }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement