Skip to content

Instantly share code, notes, and snippets.

@davidfowl
Last active February 6, 2019 10:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidfowl/530c5d98c1ae216fac3df474a7914238 to your computer and use it in GitHub Desktop.
Save davidfowl/530c5d98c1ae216fac3df474a7914238 to your computer and use it in GitHub Desktop.
Count up from 0 to {count}, showing an item every {delay} milliseconds
public IObservable<int> ObservableCounter(int count, int delay)
{
return Observable.Range(0, count).Zip(
Observable.Interval(TimeSpan.FromMilliseconds(delay)), (item, _) => item);
}
@davidfowl
Copy link
Author

This works as well

return Observable.Interval(TimeSpan.FromMilliseconds(delay))
                             .Select((_, index) => index)
                             .Take(count);

@CoreyKaylor
Copy link

How about...

return Observable.Range(0, count)
                .Delay(TimeSpan.FromSeconds(delay));

Although wasn't in a position to test, I think this will work.

@mattpodwysocki
Copy link

public IObservable<int> ObservableCounter(int count, int delay)
{
    return Observable.For(Observable.Range(0, count), i => Observable.Return(i).Delay(TimeSpan.FromMilliseconds(delay)));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment