Created
July 28, 2016 21:11
-
-
Save metric-space/15acf747b8fa1d4b3ea61e8f21c371c2 to your computer and use it in GitHub Desktop.
simple counter with reset using rxjs
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.6/rx.all.js"></script> | |
<title>Rxjs start</title> | |
</head> | |
<body> | |
<button id="egg">Mash</button> | |
<button id="reset">Reset</button> | |
<div id="display">0</> | |
<script> | |
var eg = document.getElementById("egg"); | |
var display = document.getElementById("display"); | |
var reset = document.getElementById("reset"); | |
var subreset = Rx.Observable.fromEvent(reset,'click') | |
.map(function(x){return 'reset';}) | |
var submash = Rx.Observable.fromEvent(eg,'click') | |
.map(function(x){ return 1;}) | |
var sub = Rx.Observable.merge(subreset, submash) | |
.scan(function(acc,x) {return (x == 'reset')?0:acc+x;},1); | |
sub.subscribe(function(x){ | |
display.innerHTML = x; | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment