Skip to content

Instantly share code, notes, and snippets.

@bobpace
Created March 18, 2015 23:23
Show Gist options
  • Save bobpace/2e6f926f5dbc9afd6caa to your computer and use it in GitHub Desktop.
Save bobpace/2e6f926f5dbc9afd6caa to your computer and use it in GitHub Desktop.
Random interval observable
var Rx = require('rx');
var _ = require('lodash');
module.exports = function(minSeconds, maxSeconds) {
if (minSeconds >= maxSeconds) {
throw new Error('min needs to be less than max');
}
var getDueTime = () => _.random(minSeconds, maxSeconds) * 1000;
return new Rx.AnonymousObservable((observer) => {
return Rx.Scheduler.timeout.scheduleRecursiveWithRelative(
getDueTime(),
(self) => {
observer.onNext();
self(getDueTime());
}
)
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment