[RxJS] Creation operators: interval and timer

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.

Interval(period):

You can create interval() function by you own:

var foo = Rx.Observable.create( function(Observe){
var i = ;
setInterval(function(){
Observe.next(i);
i++;
}, );
}) foo.subscribe(function (x) {
console.log('next ' + x);
}, function (err) {
console.log('error ' + err);
}, function () {
console.log('done');
});

Or:

var foo = Rx.Observable.interval();

foo.subscribe(function (x) {
console.log('next ' + x);
}, function (err) {
console.log('error ' + err);
}, function () {
console.log('done');
});

timer(delay/date, period):

var foo = Rx.Observable.timer(, ); // after 3 second delay
var date = new Date(new Date().getTime() + );
var foo = Rx.Observable.timer(date, ); // accept a date object foo.subscribe(function (x) {
console.log('next ' + x);
}, function (err) {
console.log('error ' + err);
}, function () {
console.log('done');
});
上一篇:CSS速查列表-2-(text)文本


下一篇:基于ZedBoard的Webcam设计(一):USB摄像头(V4L2接口)的图片采集【转】