import { Injectable } from '@angular/core' ; import * as countdown from 'countdown' ; import { Observable , Observer } from 'rxjs' ; import { getFormatedTime } from 'app/shared/helpers/time.helper' ; @ Injectable ( ) export class CountdownService { constructor ( ) { } getCountdownTo ( hour , minute ) { const now = new Date ( ) ; let previousSecond ; return Observable . create ( ( observer ) => { const timerId = countdown ( new Date ( now . getFullYear ( ) , now . getMonth ( ) , now . getDate ( ) , hour , minute , 0 ) , ( ts ) => { const time = getFormatedTime ( ts ) ; if ( previousSecond && previousSecond > ts . seconds ) { observer . next ( time ) ; } if ( ( ts . hours === 0 && ts . minutes === 0 && ts . seconds === 0 ) || ( previousSecond && previousSecond < ts . seconds ) ) { window . clearInterval ( timerId ) ; observer . complete ( ) ; } previousSecond = ts . seconds ; } , countdown . HOURS | countdown . MINUTES | countdown . SECONDS ) ; return ( ) => { window . clearInterval ( timerId ) ; } ; } ) ; } }