Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

Display digital clock in angular

// in ts file

import { Component, OnInit } from '@angular/core';
import { Subscription, timer } from 'rxjs';
import { map, share } from "rxjs/operators";
import { OnDestroy } from '@angular/core';


@Component({
  selector: 'app-clock',
  templateUrl: './clock.component.html',
  styleUrls: ['./clock.component.scss']
})
export class ClockComponent implements OnInit, OnDestroy {

  date: Date = new Date();

  constructor() {
  }

  time = new Date();
  rxTime = new Date();
  intervalId;
  subscription: Subscription;

  ngOnInit() {
    // Using Basic Interval
    this.intervalId = setInterval(() => {
      this.time = new Date();
    }, 1000);

    // Using RxJS Timer
    this.subscription = timer(0, 1000)
      .pipe(
        map(() => new Date()),
        share()
      )
      .subscribe(time => {
        this.rxTime = time;
      });
  }

  ngOnDestroy() {
    clearInterval(this.intervalId);
    if (this.subscription) {
      this.subscription.unsubscribe();
    }
  }

}

// in html
 Simple Clock:
 <div>{{ time | date: 'hh:mm:ss a' }}</div>
 RxJS Clock:
 <div>{{ rxTime | date: 'hh:mm:ss a' }}</div>
Comment

PREVIOUS NEXT
Code Example
Typescript :: benefits of multiprogramming 
Typescript :: cannot be used as a jsx component 
Typescript :: adonis model use transaction 
Typescript :: psycopg2 OperationalError: FATAL: unsupported frontend protocol 1234.5679: server supports 2.0 to 3.0 
Typescript :: count objects in selenium java 
Typescript :: why do giant covalent structures have high boiling points 
Typescript :: get slope from two points 
Typescript :: useref typescript 
Typescript :: facts aboutdavid oliveira 
Typescript :: sklearn tsne 
Typescript :: react-scripts start error 
Typescript :: nestjs mongoose with config 
Typescript :: full call signature in ts 
Typescript :: nodejs jszip create zip file as buffer 
Typescript :: collapse all code vscode 
Typescript :: loop through object typescript 
Typescript :: angular typescript set meta data 
Typescript :: react native typescript 
Typescript :: armstrong number program in typescript 
Typescript :: foreach typescript 
Typescript :: ts(7053) 
Typescript :: how to find uncommon elements in two lists in python 
Typescript :: unable to connect to postgresql server fatal password authentication failed for user 
Typescript :: typescript get all enum values 
Typescript :: add key value pair to all objects in array 
Typescript :: how to check if an entry exists in a model django 
Typescript :: react routes not working after build 
Typescript :: how to check if a string is composed only of alphabets in python 
Typescript :: linq check if exists in list 
Typescript :: what does virtual assistants do? 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =