Die*_*nue 17 javascript angular
我希望有一个像这样的日期倒计时:
http://codepen.io/garethdweaver/pen/eNpWBb
但是在角度2中,我发现这个plunkr每500毫秒增加一个数字:
https://plnkr.co/edit/pVMEbbGSzMwSBS4XEXJI?p=preview
这是代码:
import {Component,Input} from 'angular2/core';
import {Observable} from 'rxjs/Rx';
@Component({
selector: 'my-app',
template: `
<div>
{{message}}
</div>
`
})
export class AppComponent {
constructor() {
Observable.interval(1000)
.map((x) => x+1)
.subscribe((x) => {
this.message = x;
}):
}
}
Run Code Online (Sandbox Code Playgroud)
但我希望约会一秒钟,直到达到0.
Kno*_*per 28
import { Component, NgOnInit, ElementRef, OnInit, OnDestroy } from 'angular2/core';
import { Observable, Subscription } from 'rxjs/Rx';
@Component({
selector: 'my-app',
template: `
<div>
{{message}}
</div>
`
})
export class AppComponent implements OnInit, OnDestroy {
private future: Date;
private futureString: string;
private counter$: Observable<number>;
private subscription: Subscription;
private message: string;
constructor(elm: ElementRef) {
this.futureString = elm.nativeElement.getAttribute('inputDate');
}
dhms(t) {
var days, hours, minutes, seconds;
days = Math.floor(t / 86400);
t -= days * 86400;
hours = Math.floor(t / 3600) % 24;
t -= hours * 3600;
minutes = Math.floor(t / 60) % 60;
t -= minutes * 60;
seconds = t % 60;
return [
days + 'd',
hours + 'h',
minutes + 'm',
seconds + 's'
].join(' ');
}
ngOnInit() {
this.future = new Date(this.futureString);
this.counter$ = Observable.interval(1000).map((x) => {
return Math.floor((this.future.getTime() - new Date().getTime()) / 1000);
});
this.subscription = this.counter$.subscribe((x) => this.message = this.dhms(x));
}
ngOnDestroy(): void {
this.subscription.unsubscribe();
}
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<my-app inputDate="January 1, 2018 12:00:00">Loading...</my-app>
Run Code Online (Sandbox Code Playgroud)
这是一个带有硬编码日期的简化版本,并返回四个不同的输出(天 - 小时 - 分钟 - 秒),您可以很好地放在四个框中:
export class HomePage {
// Hardcoded date
private eventDate: Date = new Date(2018, 9, 22);
private diff: number;
private countDownResult: number;
private days: number;
private hours: number;
private minutes: number;
private seconds: number;
constructor(public navCtrl: NavController ) {
Observable.interval(1000).map((x) => {
this.diff = Math.floor((this.eventDate.getTime() - new Date().getTime()) / 1000);
}).subscribe((x) => {
this.days = this.getDays(this.diff);
this.hours = this.getHours(this.diff);
this.minutes = this.getMinutes(this.diff);
this.seconds = this.getSeconds(this.diff);
});
}
getDays(t){
return Math.floor(t / 86400);
}
getHours(t){
const days = Math.floor(t / 86400);
t -= days * 86400;
const hours = Math.floor(t / 3600) % 24;
return hours;
}
getMinutes(t){
const days = Math.floor(t / 86400);
t -= days * 86400;
const hours = Math.floor(t / 3600) % 24;
t -= hours * 3600;
const minutes = Math.floor(t / 60) % 60;
return minutes;
}
getSeconds(t){
const days = Math.floor(t / 86400);
t -= days * 86400;
const hours = Math.floor(t / 3600) % 24;
t -= hours * 3600;
const minutes = Math.floor(t / 60) % 60;
t -= minutes * 60;
const seconds = t % 60;
return seconds;
}
}
Run Code Online (Sandbox Code Playgroud)
对Johnnyfittizio的回答做了一些修改./sf/answers/2854917761/
import {Observable} from 'rxjs/Rx';
import {Component, OnInit} from '@angular/core';
export class TimerComponent implements OnInit {
private _trialEndsAt;
private _diff: number;
private _days: number;
private _hours: number;
private _minutes: number;
private _seconds: number;
constructor() {}
ngOnInit() {
this._trialEndsAt = "2017-02-28";
Observable.interval(1000).map((x) => {
this._diff = Date.parse(this._trialEndsAt) - Date.parse(new Date().toString());
}).subscribe((x) => {
this._days = this.getDays(this._diff);
this._hours = this.getHours(this._diff);
this._minutes = this.getMinutes(this._diff);
this._seconds = this.getSeconds(this._diff);
});
}
getDays(t){
return Math.floor( t/(1000*60*60*24) );
}
getHours(t){
return Math.floor( (t/(1000*60*60)) % 24 );
}
getMinutes(t){
return Math.floor( (t/1000/60) % 60 );
}
getSeconds(t){
return Math.floor( (t/1000) % 60 );
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28163 次 |
| 最近记录: |