码迷,mamicode.com
首页 > Web开发 > 详细

[RxJS] Transformation operators: delay and delayWhen

时间:2016-05-27 20:01:18      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

This lessons teaches about delay and delayWhen: simple operators that time shift.

 

delay(number | date)

var foo = Rx.Observable.interval(500).take(5);

/*
--0--1--2--3--4|
 delay(1000)
-----0--1--2--3--4|
*/

// delay(1000)
var result = foo.delay(1000);

result.subscribe(
  function (x) { console.log(‘next ‘ + x); },
  function (err) { console.log(‘error ‘ + err); },
  function () { console.log(‘done‘); },
);
var foo = Rx.Observable.interval(500).take(5);

/*
--0--1--2--3--4|
 delay(date)
-----0--1--2--3--4|
*/


var date = new Date(new Date().getTime() + 1000); 
var result = foo.delay(date);

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

 

 

delayWhen( function :Observable): accept a function which return an observable:

var foo = Rx.Observable.interval(500).take(5);

/*
--0--1--2--3--4|
 delayWhen(x => --------0--------...)
--------0--------1--------2--------3--------4|
*/

// delay(1000)
var result = foo.delayWhen(x =>
  Rx.Observable.interval(x * 1000) // For each foo, it will delay 1000 * x, so ‘2‘ --> 2000, ‘3‘ ---> 3000
);

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

 

[RxJS] Transformation operators: delay and delayWhen

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5535700.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!