我想为angular做个计时器。它应该像秒表一样,还可以暂停、恢复、停止、启动它,以及从哪个指定的计时器开始计时。
我使用了<countup-timer>
,但它不能正常工作,它不能从我指定的日期/时间开始。
请帮帮我
https://www.npmjs.com/package/ngx-timer我正在使用这个演示,但它不能按我需要的那样工作
发布于 2020-01-27 13:02:37
库在这里正常工作是在
应用程序component.html
<h3>2. Countdown Timer</h3>
<countdown-timer [countDownTimerConfig]="testConfig"></countdown-timer>
<br>
<button (click)="startTimerGT()">Start with a given time</button>
<button (click)="pauseTimer()">pause</button>
<button (click)="resumeTimer()">Resume</button>
<button (click)="stopTimer()">Stop</button>
在app.component.ts中
import { Component } from '@angular/core';
import {CountdownTimerService } from 'ngx-timer'
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
constructor(private countdownTimerService: CountdownTimerService){
}
name = 'Angular';
startTimerGT = ()=>{
this.stopTimer();
let cdate = new Date(); // desired date
cdate.setHours(cdate.getHours() + 1);
cdate.setSeconds(cdate.getSeconds() + 5);
this.countdownTimerService.startTimer(cdate);
}
stopTimer = () =>{
this.countdownTimerService.stopTimer();
}
resumeTimer = () =>{
this.countdownTimerService.resumeTimer();
}
pauseTimer = () =>{
this.countdownTimerService.pauseTimer();
}
}
app.module.ts
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import { NgxTimerModule } from 'ngx-timer';
@NgModule({
imports: [ BrowserModule, FormsModule ,NgxTimerModule],
declarations: [ AppComponent, HelloComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
发布于 2020-11-09 18:09:55
有一个名为"srr-elapsedtimer“的库,你可以用它来解决这个问题
首先,使用以下代码安装库
npm i srr-elapsedtimer
之后,如下所示配置您的app.module.ts
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
// Import library module
import { ElapsedtimerModule } from "srr-elapsedtimer";
@NgModule({
imports: [
// ...
ElapsedtimerModule
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class AppModule {}
Add ElapsedtimerService service wherever you want to use the srr-elapsedtimer.
import { ElapsedtimerService } from "srr-elapsedtimer";
class AppComponent implements OnInit {
constructor(private timer: ElapsedtimerService) {}
ngOnInit() {
this.timer.startTimer(); //This is simple example on how we can use a function
}
}
将这段代码放入html文件中。
<srr-elapsedtimer></srr-elapsedtimer>
最后,像下面这样准备你的component.ts
import { ElapsedtimerService } from "srr-elapsedtimer";
class AppComponent implements OnInit {
constructor(private timer: ElapsedtimerService) {}
ngOnInit() {
this.timer.startTimer(); //This is simple example on how we can use a function
}
}
要暂停、恢复和停止,您可以相应地使用以下方法
this.timer.pauseTimer() //Pause the timer
this.timer.resumeTimer() //Resume the timer
this.timer.resetTimer() //Reset the timer
要延迟启动计时器,可以使用以下方法
this.timer.delayStart(seconds:number)
有关更多信息,请参阅此!https://www.npmjs.com/package/srr-elapsedtimer
https://stackoverflow.com/questions/59929849
复制相似问题