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');
});

[RxJS] Creation operators: interval and timer的更多相关文章

  1. [RxJS] Creation operators: empty, never, throw

    This lesson introduces operators empty(), never(), and throw(), which despite being plain and void o ...

  2. [RxJS] Creation operators: fromEventPattern, fromEvent

    Besides converting arrays and promises to Observables, we can also convert other structures to Obser ...

  3. [RxJS] Creation operators: from, fromArray, fromPromise

    The of() operator essentially converted a list of arguments to an Observable. Since arrays are often ...

  4. [RxJS] Creation operator: of()

    RxJS is a lot about the so-called "operators". We will learn most of the important operato ...

  5. [RxJS] Transformation operators: debounce and debounceTime

    Debounce and debounceTime operators are similar to delayWhen and delay, with the difference that the ...

  6. [RxJS] Transformation operators: delay and delayWhen

    This lessons teaches about delay and delayWhen: simple operators that time shift. delay(number | dat ...

  7. [RxJS] Filtering operators: takeLast, last

    Operators take(), skip(), and first() all refer to values emitted in the beginning of an Observable ...

  8. [RxJS] Filtering operators: take, first, skip

    There are more operators in the filtering category besides filter(). This lesson will teach how take ...

  9. [RxJS] Creation operator: create()

    We have been using Observable.create() a lot in previous lessons, so let's take a closer look how do ...

随机推荐

  1. SQLServer2008收缩数据库日志

    -- Set to SIMPLE mode ALTER DATABASE [DATABASE_NAME] SET RECOVERY SIMPLE; -- Shrink the db ); -- Set ...

  2. CSS代码语法

    css 样式由选择符和声明组成,而声明又由属性和值组成,如下图所示: 选择符:又称选择器,指明网页中要应用样式规则的元素,如本例中是网页中所有的段(p)的文字将变成蓝色,而其他的元素(如ol)不会受到 ...

  3. Hibernate 环境搭建

    Hibernate 工作流程 1.创建工程并导包 2.在src根目录下创建配置文件:hibernate.cfg.xml(也可以创建在src其他文件夹下,但是在后面的配置中,需要指明路径) <?x ...

  4. python下 help()使用方法

    查看python所有的modules:help("modules") 单看python所有的modules中包含指定字符串的modules: help("modules ...

  5. bash shell学习-实践 (自己实现一些小工具)

    The poor starve while the rich feast. "穷人饥肠辘辘,富人大吃大喝" 参考资料:鸟哥的Linux私房菜 基础学习篇(第三版)  Linux S ...

  6. jQuery toggle() 方法与实例以及代替方法。

    看<jQeury 权威指南>时看到这个toggle()方法.因为之前在慕课网学习接触过.发现两者讲的有细微的不同 以隐藏/显示目标元素效果为例,慕课网是这样讲解的 $("#cli ...

  7. javascript关于原型的深刻理解

    Javascript继承机制的设计思想   作者: 阮一峰 日期: 2011年6月 5日 我一直很难理解Javascript语言的继承机制. 它没有"子类"和"父类&qu ...

  8. phpMemcache消息队列类

    <?php /** * Memcache 消息队列类 */ class QMC { const PREFIX = 'ASDFASDFFWQKE'; /** * 初始化 mc * @staticv ...

  9. DNS递归和迭代原理

    11.3.7 DNS递归解析原理 “递归解析”(或叫“递归查询”,其实意思是一样的)是最常见,也是默认的解析方式.在这种解析方式中,如果客户端配置的本地名称服务器不能解析的话,则后面的查询全由本地名称 ...

  10. JS 对象、HTML事件处理、JS 类型转换、Date

    1. JS 对象 <script> var Person = new Object(); Person.id = 1; Person.name = "Hello World&qu ...