Skip to content

Instantly share code, notes, and snippets.

@muhammadadilmalik
Last active June 2, 2022 00:59
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save muhammadadilmalik/52fa872076cec56d7e3a773ede1f42d7 to your computer and use it in GitHub Desktop.
Save muhammadadilmalik/52fa872076cec56d7e3a773ede1f42d7 to your computer and use it in GitHub Desktop.
<div class="row" style="margin-bottom: 12px;">
<div class="col-md-12" style="text-align: center;">
This page will refresh in <div class="time">{{minutes}}</div> Min <div class="time">{{seconds}}</div> Sec
</div>
</div>
.time {
background-color: #FC5B2D;
color: white;
width: 25px;
height: 20px;
display: inline-block;
border-radius: 5px;
}
import { Component, OnInit, Output, Input, EventEmitter, ChangeDetectorRef } from '@angular/core';
import { Subscription, Observable, timer } from 'rxjs';
import * as moment from 'moment';
@Component({
selector: 'kt-auto-refresh',
templateUrl: './auto-refresh.component.html',
styleUrls: ['./auto-refresh.component.scss']
})
export class AutoRefreshComponent implements OnInit {
private subscription: Subscription;
@Output() TimerExpired: EventEmitter<any> = new EventEmitter<any>();
@Input() SearchDate: moment.Moment = moment();
@Input() ElapsTime: number = 3;
searchEndDate: moment.Moment;
remainingTime: number;
minutes: number;
seconds: number;
everySecond: Observable<number> = timer(0, 1000);
constructor(private ref: ChangeDetectorRef) {
this.searchEndDate = this.SearchDate.add(this.ElapsTime, "minutes");
}
ngOnInit() {
this.subscription = this.everySecond.subscribe((seconds) => {
var currentTime: moment.Moment = moment();
this.remainingTime = this.searchEndDate.diff(currentTime)
this.remainingTime = this.remainingTime / 1000;
if (this.remainingTime <= 0) {
this.SearchDate = moment();
this.searchEndDate = this.SearchDate.add(this.ElapsTime, "minutes");
this.TimerExpired.emit();
}
else {
this.minutes = Math.floor(this.remainingTime / 60);
this.seconds = Math.floor(this.remainingTime - this.minutes * 60);
}
this.ref.markForCheck()
})
}
ngOnDestroy(): void {
this.subscription.unsubscribe();
}
}
everySecond: Observable<number> = timer(0, 1000);
this.subscription = this.everySecond.subscribe((seconds) => {
var currentTime: moment.Moment = moment();
this.remainingTime = this.searchEndDate.diff(currentTime)
this.remainingTime = this.remainingTime / 1000;
if (this.remainingTime <= 0) {
this.SearchDate = moment();
this.searchEndDate = this.SearchDate.add(this.ElapsTime, "minutes");
this.TimerExpired.emit();
}
else {
this.minutes = Math.floor(this.remainingTime / 60);
this.seconds = Math.floor(this.remainingTime - this.minutes * 60);
}
this.ref.markForCheck()
})
@Amala-Dharmana
Copy link

where we have to write samplets in Visual Studio Code.

@AlexZeaiter
Copy link

What is moment that you imported ?

@amalik-poit
Copy link

What is moment that you imported ?

https://www.npmjs.com/package/moment (This is the package, it provides various useful functions related to date and time)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment