博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[RxJS] Adding Conditional Logic with Filter
阅读量:5146 次
发布时间:2019-06-13

本文共 1770 字,大约阅读时间需要 5 分钟。

Often you only want values to proceed through your stream if they meet certain criteria, just as if you were using an if statement in plain JavaScript. This lesson shows you how to use filter on your stream to only push the values that you need through your stream.

 

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));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))Observable.combineLatest(    timer$,    input$,    (timer, input)=> ({count: timer.count, text: input}))    .filter((data)=> data.count === parseInt(data.text))    .subscribe(        (x)=> console.log(x),        err=> console.log(err),        ()=> console.log('complete')    );

 

转载于:https://www.cnblogs.com/Answer1215/p/5265924.html

你可能感兴趣的文章
oracle 创建暂时表
查看>>
201421410014蒋佳奇
查看>>
Xcode5和ObjC新特性
查看>>
LibSVM for Python 使用
查看>>
Centos 7.0 安装Mono 3.4 和 Jexus 5.6
查看>>
CSS属性值currentColor
查看>>
java可重入锁reentrantlock
查看>>
浅谈卷积神经网络及matlab实现
查看>>
解决ajax请求cors跨域问题
查看>>
《收获,不止Oracle》pdf
查看>>
Real-Time Rendering 笔记
查看>>
如何理解HTML结构的语义化
查看>>
Activity之间的跳转:
查看>>
实验四2
查看>>
多路复用
查看>>
Python数据可视化之Pygal(雷达图)
查看>>
Java学习笔记--字符串和文件IO
查看>>
转 Silverlight开发历程—(画刷与着色之线性渐变画刷)
查看>>
SQL语法(3)
查看>>
在js在添版本号
查看>>