Last active
April 22, 2016 22:02
-
-
Save amoilanen/03ca368e68294d0721e5695dc329e2a3 to your computer and use it in GitHub Desktop.
Possible implementation of Rx.js `interval` function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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