Skip to content

Instantly share code, notes, and snippets.

@amoilanen
Last active April 22, 2016 22:02
Show Gist options
  • Save amoilanen/03ca368e68294d0721e5695dc329e2a3 to your computer and use it in GitHub Desktop.
Save amoilanen/03ca368e68294d0721e5695dc329e2a3 to your computer and use it in GitHub Desktop.
Possible implementation of Rx.js `interval` function
Rx.Observable.interval = function(timeout) {
var DEFAULT_TIMEOUT_MS = 1000;
timeout = timeout || DEFAULT_TIMEOUT_MS;
return Rx.Observable.create(function(observer) {
var i = 0;
var interval = setInterval(function() {
observer.onNext(i++);
}, timeout);
});
};
/*
* Usage example
*/
var subscription = Rx.Observable.interval(100).subscribe(
function onNext(x) {
console.log('Next value = ', x);
},
function onError() {},
function onCompleted() {
console.log('Completed');
}
);
setTimeout(function() {
subscription.dispose();
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment