首页 > Web开发 > 详细

[RxJS] Creation operators: interval and timer

时间:2016-04-17 17:36:58      阅读:283      评论:0      收藏:0      [点我收藏+]

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 = 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:

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):

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

 

[RxJS] Creation operators: interval and timer

原文:http://www.cnblogs.com/Answer1215/p/5401372.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!