如何在angular2中创建计时器

kun*_*tal 93 javascript angular

我需要Angular 2中的一个计时器,它在一个时间间隔后打勾并完成一些任务(可能会调用一些函数).

如何用Angular 2做到这一点?

Abd*_*yer 129

除了之前的所有答案,我还是会使用RxJS Observables

请检查Observable.timer

这是一个示例代码,将在2秒后启动,然后每秒滴答:

import {Component} from 'angular2/core';
import {Observable} from 'rxjs/Rx';

@Component({
    selector: 'my-app',
    template: 'Ticks (every second) : {{ticks}}'
})
export class AppComponent {
  ticks =0;
  ngOnInit(){
    let timer = Observable.timer(2000,1000);
    timer.subscribe(t=>this.ticks = t);
  }
}
Run Code Online (Sandbox Code Playgroud)

这是一个工作的plunker

更新 如果要调用AppComponent类上声明的函数,可以执行以下操作之一:

**假设您要调用的函数名为func,

ngOnInit(){
    let timer = Observable.timer(2000,1000);
    timer.subscribe(this.func);
}
Run Code Online (Sandbox Code Playgroud)

上述方法的问题在于,如果在func中调用'this',它将引用订阅者对象而不是AppComponent对象,这可能不是您想要的.

但是,在下面的方法中,您创建一个lambda表达式并在其中调用函数func.这样,对func的调用仍然在AppComponent的范围内.在我看来,这是最好的方法.

ngOnInit(){
    let timer = Observable.timer(2000,1000);
    timer.subscribe(t=> {
        this.func(t);
    });
}
Run Code Online (Sandbox Code Playgroud)

检查这个plunker的工作代码.


Phi*_*nci 79

另一种解决方案是使用TimerObservable

TimerObservable是Observable的子类.

import {Component, OnInit, OnDestroy} from '@angular/core';
import {Subscription} from "rxjs";
import {TimerObservable} from "rxjs/observable/TimerObservable";

@Component({
  selector: 'app-component',
  template: '{{tick}}',
})
export class Component implements OnInit, OnDestroy {

  private tick: string;
  private subscription: Subscription;

  constructor() {
  }

  ngOnInit() {
    let timer = TimerObservable.create(2000, 1000);
    this.subscription = timer.subscribe(t => {
      this.tick = t;
    });
  }

  ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}
Run Code Online (Sandbox Code Playgroud)

PS:别忘了取消取消.

  • 取消订阅部分是关键 (3认同)
  • @Notflip ngOnDestroy 在组件取消初始化期间被调用:`this.subscription.unsubscribe();` 取消订阅。 (2认同)
  • 如何暂停并重新启动它? (2认同)

小智 11

import {Component, View, OnInit, OnDestroy} from "angular2/core";

import { Observable, Subscription } from 'rxjs/Rx';

@Component({

})
export class NewContactComponent implements OnInit, OnDestroy {

    ticks = 0;
    private timer;
    // Subscription object
    private sub: Subscription;


    ngOnInit() {
        this.timer = Observable.timer(2000,5000);
        // subscribing to a observable returns a subscription object
        this.sub = this.timer.subscribe(t => this.tickerFunc(t));
    }
    tickerFunc(tick){
        console.log(this);
        this.ticks = tick
    }

    ngOnDestroy(){
        console.log("Destroy timer");
        // unsubscribe here
        this.sub.unsubscribe();

    }


}
Run Code Online (Sandbox Code Playgroud)

  • 这不会添加任何其他答案中尚未解释的内容,或者是这样吗? (3认同)

Mat*_*ers 7

使用 rxjs 6.2.2 和 Angular 6.1.7,我得到了:

Observable.timer is not a function

错误。这是通过替换Observable.timertimer

import { timer, Subscription } from 'rxjs';

private myTimerSub: Subscription;    

ngOnInit(){    
    const ti = timer(2000,1000);    
    this.myTimerSub = ti.subscribe(t => {    
        console.log("Tick");    
    });    
}    

ngOnDestroy() {    
    this.myTimerSub.unsubscribe();    
}
Run Code Online (Sandbox Code Playgroud)

  • 要取消订阅,您必须有一个订阅变量,如上面的 @alexis-poo。请参阅:/sf/ask/2812681861/。我基于您的回答在这方面没有按书面方式工作。 (3认同)