简体   繁体   English

如何让倒数计时器在 Angular 10 中工作

[英]How can I get a Countdown Timer to work in Angular 10

I am attempting to create an Angular-Nativescript shared app that executes some function when a timer reaches zero.我正在尝试创建一个 Angular-Nativescript 共享应用程序,它在计时器达到零时执行一些 function。 I would also like the timer to restart/reset when the end value is changed.我还希望计时器在结束值更改时重新启动/重置。

In Angular 9, this functionality was working with the angular-countdown-timer package.在 Angular 9 中,此功能与角度倒数计时器package 一起使用。

However, since upgrading to Angular 10, I can't get past the ModuleWithProviders requiring some generic type issue.但是,自升级到ModuleWithProviders 10 后,我无法通过需要一些通用类型问题的 ModuleWithProviders。

I have also tried using angular8-countdown-timer which works when the end value is set on page load but not afterwards (even though the end value in the component is changing).我也尝试过使用angular8-countdown-timer ,它可以在页面加载时设置结束值时工作,但之后不会(即使组件中的结束值正在改变)。

I also tried some of the older packages like ngx-countdown-timer but no joy as they general fall foul of the ModuleWithProviders restriction.我还尝试了一些较旧的软件包,如ngx-countdown-timer ,但没有任何乐趣,因为它们通常会ModuleWithProviders限制。

I've tried using ModuleWithProviders<any> and ModuleWithProviders<unknown> as I've seen suggested in similar posts but the compiler just laughs in my face.我已经尝试使用ModuleWithProviders<any>ModuleWithProviders<unknown>正如我在类似帖子中看到的那样,但编译器只是当着我的面笑。

Any help with any of the above or even a way to contact the authors would be appreciated.对上述任何一项的任何帮助,甚至联系作者的方式,我们都将不胜感激。

I am not sure if I understand your question completely, but I just updated my own angular9 project to use a slightly more efficient timer, where I am not importing all of rxjs.我不确定我是否完全理解你的问题,但我刚刚更新了我自己的 angular9 项目以使用稍微更高效的计时器,我没有导入所有 rxjs。

I use several counters on the page and serveral resets.我在页面上使用了几个计数器并重置了几个计数器。

Essentially when I start a timer I generate a new one, and do work when the interval elapses, and then stop the subscription when the stop button is clicked.本质上,当我启动一个计时器时,我会生成一个新计时器,并在间隔结束时开始工作,然后在单击停止按钮时停止订阅。 In order to do what you want, I would guess you just call the .unsubscribe() function from inside the "do stuff" block.为了做你想做的事,我猜你只需从“做事”块内调用.unsubscribe() function 即可。

I hope this helps you, or others coming here looking to time stuff in angular 8-10我希望这可以帮助你,或者其他来这里寻找时间的东西 angular 8-10


import { Subscription, timer } from 'rxjs';

...
  subscription: Subscription;


  startTimer(){
    const source = timer(1000, 2000);
    this.subscription = source.subscribe(val => {
       // do stuff you want when the interval ticks
    }
  }

  stopTimer(){
    this.subscription.unsubscribe();
  }

My inspiration is this article .我的灵感来自这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM