rxjs debounceTime减少搜索的频率

debounceTime用来降低事件的触发频率 ,接收以毫秒为单位的参数

它所做的操作是,在一定时间范围内不管产生了多少事件,它只放第一个过去,剩下的都将舍弃

html:

<div class="autocomplete">
  <input class="input" type="search" id="search" autocomplete="off" placeholder="autocomplete">
  <ul id="suggest-list" class="suggest"></ul>
</div>

js代码: 

getSuggestList 是一个发送 ajax 请求的方法,返回 promise,我们使用 from 来将其转化为 Observable。 
为了优化请求,首先 e.target.value 是空字符串时不应该发送请求,
然后可以使用 debounceTime 减少触发频率,也可以使用 distinctUntilChanged 操作符来表示只有与上次不同时才去发送请求。
我们还可以在 API 失败时重试 3 次。

import { fromEvent } from 'rxjs';
import { debounceTime,map,distinctUntilChanged,switchMap} from 'rxjs/operators'; // merge

const input = document.querySelector('#search');
const suggestList = document.querySelector('#suggest-list');
const input$ = fromEvent(input, 'input');
input$.pipe(
  filter(e => e.target.value.length > 1),
  debounceTime(300),
  distinctUntilChanged(),
  switchMap(
    e => from(getSuggestList(e.target.value)).pipe(retry(3))
  )
).subscribe((val)=>console.log(val))


function getSuggestList(val){
  return new Promise(function(resolve, reject){
    //异步操作
    setTimeout(()=>{
      resolve([
        {
          name:'张三1',
          age:21
        },{
          name:'张三2',
          age:22
        },{
          name:'张三3',
          age:23
        },{
          name:'张三4',
          age:24
        },{
          name:'张三5',
          age:25
        }
      ]);
    },2000)
  });
}

 

转载于:https://www.cnblogs.com/darkbluelove/p/11338233.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import { HttpClient, HttpRequest, HttpResponse, HttpParams, HttpHeaders, } from '@angular/common/http'; import { NzMessageService, NzModalService, UploadFile } from 'ng-zorro-antd'; import { _HttpClient } from '@delon/theme'; import { Route, Router, ActivatedRoute, Params } from '@angular/router'; import { FormBuilder, FormGroup } from '@angular/forms'; import { Component, OnInit } from '@angular/core'; import {throttleTime} from "rxjs/operators"; import {debounceTime} from "rxjs-compat/operator/debounceTime"; @Component({ selector: 'zjcx', templateUrl: './zjcx.component.html', }) export class ZjcxComponent implements OnInit { pageIndex = 1; pageSize = 10; total = 0; data: any[] = []; loading = false; q = { pageIndex: 1, pageSize: 10, yhzh: '', zhmc: '', dfyhzh: '', dfzhmc: '', maxje: '' , minje: '', jysj: '', }; constructor( private http: _HttpClient, private httpClient: HttpClient, public msg: NzMessageService, private modalSrv: NzModalService, public router: Router, private fb: FormBuilder, ) { } ngOnInit() { this.msg.error("暂无数据"); } searchData(reset: boolean = false) { if (reset) { this.pageIndex = 1; } const yhzh = this.q.yhzh===null ? '':this.q.yhzh; const zhmc = this.q.zhmc===null ? '':this.q.zhmc; const dfyhzh = this.q.dfyhzh===null ? '':this.q.dfyhzh; const dfzhmc = this.q.dfzhmc===null ? '':this.q.dfzhmc; const minJe = this.q.minje===null ? '':this.q.minje; const maxJe = this.q.maxje===null ? '':this.q.maxje; const jysj = this.q.jysj===null ? '':this.q.jysj; const params = new HttpParams() .set('yhzh', yhzh) .set('zhmc', zhmc) .set('dfyhzh', dfyhzh) .set('dfzhmc', dfzhmc) .set('minJe', minJe) .set('maxJe', maxJe) .set('jysj', jysj) .set('pageIndex',''+this.pageIndex) .set('pageSize', ''+this.pageSize); this.loading=true; this.http .post('/download/zjfx/zjcx/getZjxx', params) .pipe(throttleTime(1000)) .subscribe((res: any) => { this.data = res.list; this.total = res.total; this.loading = false; },() => { this.loading = false; this.msg.error('任务查询失败,请重新再试!'); }); } }根据这个代码修改一下
05-31

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值