Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div>
    <div>Seconds spent typing: <span class="timer"></span></div>
    <input type="text" id="input">
    <div class="typing"></div>
  </div>
  <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
  <script src="https://unpkg.com/@reactivex/rxjs@5.0.0-beta.12/dist/global/Rx.js"></script>
</body>
</html>
 
console.clear();
/*** Render Functions ***/
const showTyping = () =>
  $('.typing').text('User is typing...');
const showIdle = () =>
  $('.typing').text('');
const updateTimer = ms =>
  $('.timer').text(ms / 1000);
/*** Program Logic ***/
const typing$ = Rx.Observable
  .fromEvent($('#input'), 'input')
  .switchMapTo(Rx.Observable
               .never()
               .startWith('TYPING')
               .merge(Rx.Observable.timer(1000).mapTo('IDLE')))
  .do(e => e === 'TYPING' ? showTyping() : showIdle());
const isTyping$ = typing$
  .distinctUntilChanged()
  .map(e => e === "TYPING")
  .publishReplay(1)
  .refCount();
const timer$ = isTyping$
  .switchMap(isTyping => isTyping ? Rx.Observable.interval(100) : Rx.Observable.never())
  .scan(totalMs => (totalMs + 100), 0)
  .subscribe(updateTimer);
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers