admin管理员组

文章数量:1287919

I'm using this but interval operator wants to be on observable object its not exist on range, is there a way to have observable that emit for example emit 60 integer with interval of 1 second, i have been doing this

 this.clock = Observable.range(1,60);
 this.clock = this.clock.interval(1000).map(function(value){
     console.log(value)
     return value;
 })

Its saying interval is not a function

also tried this:

 this.clock = Observable.range(1,60).interval(1000).map(function(value){
     console.log(value)
     return value;
 })

I'm using this but interval operator wants to be on observable object its not exist on range, is there a way to have observable that emit for example emit 60 integer with interval of 1 second, i have been doing this

 this.clock = Observable.range(1,60);
 this.clock = this.clock.interval(1000).map(function(value){
     console.log(value)
     return value;
 })

Its saying interval is not a function

also tried this:

 this.clock = Observable.range(1,60).interval(1000).map(function(value){
     console.log(value)
     return value;
 })
Share Improve this question asked Nov 24, 2016 at 9:02 blackHawkblackHawk 6,30713 gold badges64 silver badges104 bronze badges 2
  • 2 Do you want 60 integers from 0 to 59 during 60 seconds or do you want 60 integers during 60 seconds, with every integer as a random between some range ? – maxime1992 Commented Nov 24, 2016 at 9:50
  • 1 No just sequential from 1 to 60 with time interval of 1 second – blackHawk Commented Nov 24, 2016 at 9:55
Add a ment  | 

3 Answers 3

Reset to default 6

To have a sequence from 1 to 60 with time interval of 1 second :

Observable
  .interval(1000)
  .map(x => x + 1) // to start from 1 instead of 0
  .map(x => console.log(x)) // do some logic here
  .take(60)
  .subscribe();

The output here is :

1
2
3
.
.
.
58
59
60

Here's a snippet that you can run to see the output :

// just to have the working demo on SO
let output = '';
let divToUpdate = document.getElementById('counter');

// observable
Rx.Observable
  .interval(1000)
  .map(x => x + 1) // to start from 1 instead of 0
  .map(x => {
    output = `${output}<br>${x}`;
    divToUpdate.innerHTML = output;
  })
  .take(60)
  .subscribe();
<div id="counter"></div>

<script src="https://npmcdn./[email protected]/bundles/Rx.umd.js"></script>

Angular2 : PLUNKER DEMO

export class App {
  name:string;

  constructor() {
    this.name = 'Angular2'

    let rangeObs = Observable.zip(
      Observable.range(0, 10), 
      Observable.interval(1000), 
      c => c);

    rangeObs.subscribe(i => this.name = i;);
  }
}
var clock = Observable
              .interval(100)
              .take(60)
              .map(function(value){
                console.log(value)
                return value;
              });

Use take(count).

本文标签: javascriptHow to emit integers from certain range with some interval of time using rxjsStack Overflow