Last thing to do is clean the score box and input, also auto foucs on input when click start.

const starters$ = Observable.merge(
start$.mapTo(1000),
half$.mapTo(500),
quarter$.mapTo(250)
); // Clear the score box input and focus on input
starters$.subscribe( () => {
document.querySelector('#score').innerHTML = "";
input.value = "";
input.focus();
})

--------------------------

const Observable = Rx.Observable;

const startButton = document.querySelector('#start');
const halfButton = document.querySelector('#half');
const quarterButton = document.querySelector('#quarter'); const stopButton = document.querySelector('#stop');
const resetButton = document.querySelector('#reset'); const input = document.querySelector('#input'); const start$ = Observable.fromEvent(startButton, 'click');
const half$ = Observable.fromEvent(halfButton, 'click');
const quarter$ = Observable.fromEvent(quarterButton, 'click'); const stop$ = Observable.fromEvent(stopButton, 'click');
const reset$ = Observable.fromEvent(resetButton, 'click'); const input$ = Observable.fromEvent(input, 'input')
.map(event => event.target.value); const data = {count:0};
const inc = (acc)=> ({count: acc.count + 1});
const reset = (acc)=> data; const starters$ = Observable.merge(
start$.mapTo(1000),
half$.mapTo(500),
quarter$.mapTo(250)
); // Clear the score box input and focus on input
starters$.subscribe( () => {
document.querySelector('#score').innerHTML = "";
input.value = "";
input.focus();
}) const intervalActions = (time)=> Observable.merge(
Observable.interval(time)
.takeUntil(stop$).mapTo(inc),
reset$.mapTo(reset)
); const timer$ = starters$
.switchMap(intervalActions)
.startWith(data)
.scan((acc, curr)=> curr(acc)) const runningGame$ = timer$
.do((x)=> console.log(x))
.takeWhile((data)=> data.count <= 3)
.withLatestFrom(
input$.do((x)=> console.log(x)),
(timer, input)=> ({count: timer.count, text: input})
)
.share(); // To clean the input
runningGame$
.repeat()
.subscribe( (x) => input.value = ""); runningGame$
.filter((data)=> data.count === parseInt(data.text))
.reduce((acc, curr)=> acc + 1, 0)
.repeat()
.subscribe(
(x)=> document.querySelector('#score').innerHTML = `
${x}
`,
err=> console.log(err),
()=> console.log('complete')
);
<!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.1/dist/global/Rx.umd.js"></script>
<title>JS Bin</title>
</head>
<body> <button id="start">Start</button>
<button id="half">Half</button>
<button id="quarter">Quarter</button>
<button id="stop">Stop</button>
<button id="reset">Reset</button> <hr> <input type="text" id="input"> <div>Score: <span id="score"></span></div> </body>
</html>

[RxJS] Wrap up的更多相关文章

  1. [Vue-rx] Cache Remote Data Requests with RxJS and Vue.js

    A Promise invokes a function which stores a value that will be passed to a callback. So when you wra ...

  2. RxJS + Redux + React = Amazing!(译一)

    今天,我将Youtube上的<RxJS + Redux + React = Amazing!>翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: https:/ ...

  3. RxJS + Redux + React = Amazing!(译二)

    今天,我将Youtube上的<RxJS + Redux + React = Amazing!>的后半部分翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: ht ...

  4. jQuery之常用且重要方法梳理(siblings,nextAll,end,wrap,apply,call,each)-(二)

    1.siblings() siblings() 获得匹配集合中每个元素的同胞,通过选择器进行筛选是可选的. <body> <div><span>Hello</ ...

  5. [译]RxJS 5.X基础篇

    欢迎指错与讨论 : ) 当前RxJS版本:5.0.0-beta.10.更详细的内容尽在RxJS官网http://reactivex.io/rxjs/manual/overview.html.文章比较长 ...

  6. [转载]jQuery中wrap、wrapAll和wrapInner用法以及区别

    原文地址:jQuery中wrap.wrapAll和wrapInner用法以及区别作者:伊少君 原文: <ul>   <li title='苹果'>苹果</li>   ...

  7. 学习RxJS: 导入

    原文地址:http://www.moye.me/2016/05/31/learning_rxjs_part_one_preliminary/ 引子 新手们在异步编程里跌倒时,永远会有这么一个经典问题: ...

  8. 【golang】go语言,进行并发请求的wrap变参封装

    package main import ( "fmt" "sync" "time" ) type WaitGroupWrapper stru ...

  9. jquery之wrap(),wrap(),unwrap()方法详解

    [注]wrap():为每个匹配元素外面添加指定的HTML结构, wrapAll(): 为所有匹配元素(作为一个整体)外面添加一个指定的HTML结构 原文地址:http://www.365mini.co ...

随机推荐

  1. Java学习笔记——JDBC读取properties属性文件

    Java 中的 properties 文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件. 文件的内容是格式是"键=值"(key-valu ...

  2. [转]iOS设备唯一标识探讨

    转自:http://www.jianshu.com/p/b83b0240bd0e iOS设备唯一标识探讨 为了统计和检测应用的使用数据,几乎每家公司都有获取唯一标识的业务需求,在iOS5以前获取唯一标 ...

  3. django: urlconfig

    django 的 url 配置主要在 urls.py 中进行 urlconfig 中对 url 的处理方式主要在: 一 视图处理方式 如 上文 例子所示: url(r'^blog/index/$', ...

  4. 2:numpy---ndarray

    ndarray即是多维数组[n dimension array] 一:创建ndarray 有好几种创建数组的方法. 例如,你可以使用 array 函数从常规的Python列表和元组创造数组.所创建的数 ...

  5. Geodatabase - 打开栅格数据

    打开栅格数据 和打开要素类的方式类似,打开栅格数据集需要用 IRasterWorkspace,如 //获得栅格工作空间(普通目录). public ESRI.ArcGIS.DataSourcesRas ...

  6. iOS ui界面vtf 开发

    addConstraints 添加约束的步奏 添加控件到view中 设置translateResizeLayoutintoautolayout = false 添加约束 注意 约束 : 出现 有父子关 ...

  7. FileStream操作文件读写

    FileStream fsRead = new FileStream(@"C:\Users\Administrator\Desktop\u.html",FileMode.OpenO ...

  8. POJ1276:Cash Machine(多重背包)

    Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...

  9. php对象中类的继承性访问类型控制

    类型的访问控制通过使用修饰符允许开发人员对类中成员的访问进行限制.这是PHP5的新特性,也是OOP语言中的重要特性,大多数OOP语言都已支持此特性.PHP5支持如下三种访问修饰符,在类的封装中我们已经 ...

  10. Python新手学习基础之运算符——成员运算与身份运算

    成员运算符 Python里有成员运算符,可以判断一个元素是否在某一个序列中.比如可以判断一个字符是否属于这个字符串,可以判断某个对象是否在这个列表中等等. Python中的成员操作符的使用语法是: o ...