首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >我想为angular做个计时器

我想为angular做个计时器
EN

Stack Overflow用户
提问于 2020-01-27 11:02:44
回答 2查看 704关注 0票数 1

我想为angular做个计时器。它应该像秒表一样,还可以暂停、恢复、停止、启动它,以及从哪个指定的计时器开始计时。

我使用了<countup-timer>,但它不能正常工作,它不能从我指定的日期/时间开始。

请帮帮我

https://www.npmjs.com/package/ngx-timer我正在使用这个演示,但它不能按我需要的那样工作

EN

回答 2

Stack Overflow用户

发布于 2020-01-27 13:02:37

库在这里正常工作是在

应用程序component.html

代码语言:javascript
运行
AI代码解释
复制
<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中

代码语言:javascript
运行
AI代码解释
复制
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

代码语言:javascript
运行
AI代码解释
复制
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 { }

示例工作代码也在这里https://stackblitz.com/edit/angular-vzygxv

票数 0
EN

Stack Overflow用户

发布于 2020-11-09 18:09:55

有一个名为"srr-elapsedtimer“的库,你可以用它来解决这个问题

首先,使用以下代码安装库

代码语言:javascript
运行
AI代码解释
复制
npm i srr-elapsedtimer

之后,如下所示配置您的app.module.ts

代码语言:javascript
运行
AI代码解释
复制
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文件中。

代码语言:javascript
运行
AI代码解释
复制
<srr-elapsedtimer></srr-elapsedtimer>

最后,像下面这样准备你的component.ts

代码语言:javascript
运行
AI代码解释
复制
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
 
  }
}

要暂停、恢复和停止,您可以相应地使用以下方法

代码语言:javascript
运行
AI代码解释
复制
this.timer.pauseTimer() //Pause the timer
this.timer.resumeTimer() //Resume the timer
this.timer.resetTimer() //Reset the timer

要延迟启动计时器,可以使用以下方法

代码语言:javascript
运行
AI代码解释
复制
this.timer.delayStart(seconds:number)

有关更多信息,请参阅此!https://www.npmjs.com/package/srr-elapsedtimer

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59929849

复制
相关文章

相似问题

领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文