[RxJS] Implement pause and resume feature correctly through RxJS
and Rxjs Feature Through Implement
2023-09-14 09:00:51 时间
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
相关文章
- CF1619B Squares and Cubes 题解
- Linux Platform Device and Driver「建议收藏」
- ICLR2022:Elements of Reasoning:Objects, Structure, and Causality
- ORA-23320: the request failed because of values string and string ORACLE 报错 故障修复 远程处理
- ORA-23633: Cannot turn off combined capture and apply mode in apply-state checkpoint mode (value “string”, string parameter string) ORACLE 报错 故障修复 远程处理
- ORA-38808: edition has a usable parent and a usable child ORACLE 报错 故障修复 远程处理
- ORA-56605: DRCP: Session switching and migration not allowed ORACLE 报错 故障修复 远程处理
- ORA-01126: database must be mounted in this instance and not open in any instance ORACLE 报错 故障修复 远程处理
- ORA-16185: REMOTE_ARCHIVE_ENABLE and LOG_ARCHIVE_CONFIG mutually exclusive ORACLE 报错 故障修复 远程处理
- 多条件查询MySQL中使用And多条件查询的步骤(mysql中and)
- 深入浅出:MySQL中AND和OR运算符使用方法(mysql中and和or)
- Exploring the Benefits of MySQL 5.7 Update for Enhanced Performance and Security(mysql5.7更新)
- Effective Methods to Tackle MySQL Database Corruption and Character Encoding Issues(解决mysql数据库乱码)
- Effective Strategies for Monitoring and Securing Your Linux Servers(监控linux服务器)
- MySQL中AND的使用方法解析(mysql中and的用法)
- MySQL中如何正确使用AND运算符(mysql中and怎么用)
- Oracle中使用除了And的其他查询关键字(oracle中除了and)
- Oracle 数据库中使用AND拼接的威力(oracle中and拼接)