It is quite common to need an Observable that ticks periodically, for instance every second or every 100 miliseconds. We will learn about operators interval() and timer(), both of which are similar to setInterval() in JavaScript.ui
Interval(period):spa
You can create interval() function by you own:code
var foo = Rx.Observable.create( function(Observe){ var i = 0; setInterval(function(){ Observe.next(i); i++; }, 1000); }) foo.subscribe(function (x) { console.log('next ' + x); }, function (err) { console.log('error ' + err); }, function () { console.log('done'); });
Or:blog
var foo = Rx.Observable.interval(1000); foo.subscribe(function (x) { console.log('next ' + x); }, function (err) { console.log('error ' + err); }, function () { console.log('done'); });
timer(delay/date, period):ip
var foo = Rx.Observable.timer(3000, 1000); // after 3 second delay var date = new Date(new Date().getTime() + 3000); var foo = Rx.Observable.timer(date, 1000); // accept a date object foo.subscribe(function (x) { console.log('next ' + x); }, function (err) { console.log('error ' + err); }, function () { console.log('done'); });