[RxJS] Transformation operators: delay and delayWhen

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[RxJS] Transformation operators: delay and delayWhen相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

[RxJS] Transformation operator: buffer, bufferCount, bufferTime

[RxJS] Transformation operator: map and mapTo

[RxJS] Transformation operators: delay and delayWhen

[RxJS] Transformation operator: repeat

kettle中跑transformation和job的区别

CG中的仿射(affine transformation)和法线变换