Skip to content

Instantly share code, notes, and snippets.

@johnlindquist
Created March 22, 2016 16:55
Show Gist options
  • Save johnlindquist/1185b7701932b028401b to your computer and use it in GitHub Desktop.
Save johnlindquist/1185b7701932b028401b to your computer and use it in GitHub Desktop.
const Observable = Rx.Observable;
const toggleButton = document.querySelector('#toggle');
const toggleClick$ = Observable.fromEvent(toggleButton, 'click');
const interval$ = Observable.interval(1000);
const toggle$ = toggleClick$
.startWith(false)
.scan((acc, curr)=> !acc); //toggles true/false
toggle$
//project the true/false each interval "tick"
.combineLatest(interval$, bool => bool)
//only push when true
.filter(bool=> bool)
.startWith(0)
.scan(acc => acc + 1)
.subscribe(console.log.bind(console));
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://npmcdn.com/@reactivex/rxjs@5.0.0-beta.3/dist/global/Rx.umd.js"></script>
<title>JS Bin</title>
</head>
<body>
<button id="toggle">Toggle</button>
<script src="./app.js"></script>
</body>
</html>
/* todo: add styles */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment