有一个名为“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