javascript - rxjs infinite loop with an interval variable - Stack Overflow

I want to make infinite loop with an interval variable using rxjs Observable so I'm trying to rew

I want to make infinite loop with an interval variable using rxjs Observable so I'm trying to rewrite this function in rxjs

takeAWalk(player){
    setTimeout(() => {

      console.log("Player is walking...");

      takeAWalk(player);
    }, getRandomDelayBetween(1000, 2500));
}

I tried

Observable
  .timer(0, getRandomDelayBetween(1000, 2500))
  .take(10)
  .timeInterval()
  .subscribe(res=>{
    console.log("player is walking ...");
  });

but the problem that this is finite to 10 and the interval is constant (getRandomDelayBetween is only called once).

Which operators should I use to produce the same functionality of takeAWalk function?

I want to make infinite loop with an interval variable using rxjs Observable so I'm trying to rewrite this function in rxjs

takeAWalk(player){
    setTimeout(() => {

      console.log("Player is walking...");

      takeAWalk(player);
    }, getRandomDelayBetween(1000, 2500));
}

I tried

Observable
  .timer(0, getRandomDelayBetween(1000, 2500))
  .take(10)
  .timeInterval()
  .subscribe(res=>{
    console.log("player is walking ...");
  });

but the problem that this is finite to 10 and the interval is constant (getRandomDelayBetween is only called once).

Which operators should I use to produce the same functionality of takeAWalk function?

Share asked Dec 17, 2016 at 10:41 Murhaf SousliMurhaf Sousli 13.3k22 gold badges124 silver badges191 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

There are many ways to write this in rxjs, you could try something like this:

Rx.Observable.of(null)
  .concatMap(() => Rx.Observable.timer(Math.random() * 1500))
  .do(() => console.log("player is walking ..."))
  .repeat()  // optionally .repeat(10)
  .subscribe();

Check out the example live here: http://jsbin./levakipunu/edit?js,console

Just to expand on the expand :')

This is a runWhile function I created, which acts like a while loop, with a bonus interval value, to delay the event between each loop.

It will not continue through the loop until the condition is false.

import { EMPTY, Observable, of, OperatorFunction } from 'rxjs';
import { delay, expand, filter, flatMap, mapTo } from 'rxjs/operators';

/**
 * Like a while loop for RxJS,
 * while (condition()) { doFn() }
 *
 * @param condition
 * @param doFn
 * @param interval
 * @param initialValue
 */
export function runWhile<T = any> (
  condition : () => boolean,
  doFn : () => Observable<T>,
  interval = 0,
  initialValue = null,
) : OperatorFunction<T, T> {
  return flatMap<T, T>((initial) => {
    return of(condition())
      .pipe(
        expand((cond) => {
          if (cond === false) {
            return EMPTY;
          }

          return doFn().pipe(delay(interval), mapTo(condition()));
        }),
        filter(cond => cond === false),
        mapTo(initial),
      );
  });
}

See this codepen for an example of how it works. CodePen

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742290704a4416132.html

相关推荐

  • javascript - rxjs infinite loop with an interval variable - Stack Overflow

    I want to make infinite loop with an interval variable using rxjs Observable so I'm trying to rew

    19小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信