当前位置: 动力学知识库 > 问答 > 编程问答 >

typescript - How to make observable in angular2 which updates value after interval?

问题描述:

I am creating observable in angular2.

I want to create a observable which will update the value after 2 seconds .

So far, I have created something like this.

import { Component, OnInit } from '@angular/core';

import {Observable} from 'rxjs/observable';

import 'rxjs/Rx';

@Component({

moduleId: module.id,

selector: 'observable-demo',

templateUrl: 'observabe-demo.component.html',

})

export class ObservabeDemoComponent implements OnInit {

public data:any;

public num:number;

constructor() {

//I want to update this observable value with fibonacci series after 2 seconds each.

this.data=new Observable<any>(observer=>{

setTimeout(()=>{

observer.next(32);

},2000);

});

let subscriber=this.data.subscribe(

value=>this.num=value,

err=>console.log(err),

()=>console.log("Completed")

);

}

}

Currently , after 2 seconds my observable updates the value with 32.

but I want to run this function repeatedly till value reaches 10000.

I want to store fibonacci series in this observable and print it.

is something like this possible with angular2 observables?

thanks.

网友答案:

You can use the interval() function on Observables:

return Observable
        .interval(2000) // time in ms
        .flatMap(() => {
            return nextNumber()
        });
分享给朋友:
您可能感兴趣的文章:
随机阅读: