博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[RxJS] Implement pause and resume feature correctly through RxJS
阅读量:4641 次
发布时间:2019-06-09

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

Eventually you will feel the need for pausing the observation of an Observable and resuming it later. In this lesson we will learn about use cases where pausing is possible, and what to do when pausing is impossible.

 

const resume$ = new Rx.Subject();const res$ = resume$  .switchMap(resume =>    resume ?      Rx.Observable.interval(2000) :      Rx.Observable.empty()  )  .do(x => console.log('request it! ' + x))  .switchMap(ev => Rx.Observable.ajax({    url: 'https://jsonplaceholder.typicode.com/users/1',    method: 'GET',  }));res$.subscribe(function (data) {  console.log(data.response);});resume$.next(false);setTimeout(() => resume$.next(true), 500);setTimeout(() => resume$.next(false), 5000);

 

here use 

Rx.Observable.empty()

inside switchMap(), it means if code goes to empty(), then the rest of code:

.do().switchMap()

won't run.

 

If just subscribe, it trigger complete function:

var source = Rx.Observable.empty();var subscription = source.subscribe(  function (x) {    console.log('Next: %s', x);  },  function (err) {    console.log('Error: %s', err);  },  function () {    console.log('Completed');  });  // => Completed

 

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

你可能感兴趣的文章
Java基础学习-流程控制语句
查看>>
Shell中read的常用方式
查看>>
01javascript数据类型
查看>>
asp.net实现md5加密方法详解
查看>>
AJAX
查看>>
table 的thead th 固定 tbody滚动例子
查看>>
并行计算思考----回溯法求解数独问题
查看>>
设计模式:模板模式
查看>>
和菜鸟一起学OK6410之ADC模块
查看>>
代理 模式
查看>>
[git] 细说commit (git add/commit/diff/rm/reset 以及 index 的概念)
查看>>
DOM Core和HTML DOM的区别
查看>>
SurfaceView+MediaPlay的bug们
查看>>
网络表示学习总结
查看>>
完成评论功能
查看>>
far和near
查看>>
Python爬虫实战四之抓取淘宝MM照片
查看>>
2015 Multi-University Training Contest 1
查看>>
C#判断一个字符串是否是数字或者含有某个数字
查看>>
SVN使用指南
查看>>