0

Hey! I need some help please

Here is a part of code with rxjs:

const delay = 1000

switchMap(() =>
  timer(0, delay).pipe( some logic...

I can`t change anything except the delay and as I understand periodOrScheduler can be number or SchedulerLike

export declare function timer(dueTime?: number | Date, periodOrScheduler?: number | SchedulerLike, scheduler?: SchedulerLike): Observable<number>;

So my question is:

How can I pass custom scheduler to the timer function to set delay for the first 3 repeats as 100ms and all other repeats with delay = 5000ms?

1
  • 2
    In short, no. Schedulers are not meant to be timers. They representing an execution context. This is about how pieces of code interleave with one another (which of JS's event loops, if any, is this going to appear in). You can, if you want, define your own execution context. Perhaps a minimal event loop on top of JS's existing one. But why!? It's almost certain that just re-implementing that piece of functionality from whatever library is giving you trouble would be easier than that. Commented Apr 23, 2021 at 20:24

1 Answer 1

1

How can I pass custom scheduler to the timer function to set delay for the first 3 repeats as 100ms and all other repeats with delay = 5000ms?

You could use concat to combine two separate timers, using take to limit how many the first one emits:

const delay1 = 100;
const delay2 = 5000;

switchMap(() => concat(
  timer(0, delay1).pipe(take(3)),
  interval(delay2)
))
.pipe( some logic... )
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but I can update only delay value, I can't update switchMap, this part of code is inside third party library
Do you have any ideas how to do that?
I'm not sure what code you can change. Can you show more code for context?
My question can be asked like: how to implement custom scheduler for timer function?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.