Angular實(shí)現(xiàn)防抖和節(jié)流的示例代碼
在Angular中實(shí)現(xiàn)防抖和節(jié)流的方法有多種,這篇博客主要是詳細(xì)介紹兩種常用的方法:使用RxJS操作符和使用Angular自帶的工具。
- 使用RxJS操作符實(shí)現(xiàn)防抖和節(jié)流:
防抖(Debounce):
//簡(jiǎn)易版 import { debounceTime } from 'rxjs/operators'; input.valueChanges.pipe( debounceTime(300) ).subscribe(value => { // 執(zhí)行搜索操作 }); //詳細(xì)版 import { Component } from '@angular/core'; import { fromEvent } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; @Component({ selector: 'app-debounce-example', template: '<input (input)="onInput($event)">' }) export class DebounceExampleComponent { onInput(event: Event) { fromEvent(event.target, 'input') .pipe( debounceTime(300) ) .subscribe(() => { // 執(zhí)行輸入框搜索操作 }); } }
- 節(jié)流(Throttle):
//簡(jiǎn)易版 import { throttleTime } from 'rxjs/operators'; scrollEvent.pipe( throttleTime(300) ).subscribe(() => { // 執(zhí)行滾動(dòng)操作 }); //詳細(xì)版 import { Component } from '@angular/core'; import { fromEvent } from 'rxjs'; import { throttleTime } from 'rxjs/operators'; @Component({ selector: 'app-throttle-example', template: '<div (scroll)="onScroll($event)">' }) export class ThrottleExampleComponent { onScroll(event: Event) { fromEvent(event.target, 'scroll') .pipe( throttleTime(300) ) .subscribe(() => { // 執(zhí)行滾動(dòng)操作 }); } }
- 使用Angular自帶的工具實(shí)現(xiàn)防抖和節(jié)流:
- 防抖(Debounce):
import { Component } from '@angular/core'; @Component({ selector: 'app-debounce-example', template: '<input (input)="onInput($event)">' }) export class DebounceExampleComponent { onInput(event: Event) { this.debounceSearch(); } debounceSearch = this.debounce(() => { // 執(zhí)行輸入框搜索操作 }, 300); debounce(func, delay) { let timer; return function() { clearTimeout(timer); timer = setTimeout(() => { func.apply(this, arguments); }, delay); }; } }
- 節(jié)流(Throttle):
import { Component } from '@angular/core'; @Component({ selector: 'app-throttle-example', template: '<div (scroll)="onScroll($event)">' }) export class ThrottleExampleComponent { onScroll(event: Event) { this.throttleScroll(); } throttleScroll = this.throttle(() => { // 執(zhí)行滾動(dòng)操作 }, 300); throttle(func, delay) { let canRun = true; return function() { if (!canRun) return; canRun = false; setTimeout(() => { func.apply(this, arguments); canRun = true; }, delay); }; } }
以上就是Angular實(shí)現(xiàn)防抖和節(jié)流的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Angular防抖和節(jié)流的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Angular腳手架開發(fā)的實(shí)現(xiàn)步驟
這篇文章主要介紹了Angular腳手架開發(fā)的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04AngularJS基于MVC的復(fù)雜操作實(shí)例講解
下面小編就為大家分享一篇AngularJS基于MVC的復(fù)雜操作實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12Angularjs實(shí)現(xiàn)多圖片上傳預(yù)覽功能
這篇文章主要介紹了Angularjs實(shí)現(xiàn)多圖片上傳預(yù)覽功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07詳解angular中通過(guò)$location獲取路徑(參數(shù))的寫法
本篇文章主要介紹了詳解angular中通過(guò)$location獲取路徑(參數(shù))的寫法 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Angular中使用ng-zorro圖標(biāo)庫(kù)部分圖標(biāo)不能正常顯示問(wèn)題
這篇文章主要介紹了Angular中使用ng-zorro圖標(biāo)庫(kù)部分圖標(biāo)不能正常顯示問(wèn)題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04AngularJS實(shí)現(xiàn)表單手動(dòng)驗(yàn)證和表單自動(dòng)驗(yàn)證
本文是對(duì)AngularJS表單驗(yàn)證,手動(dòng)驗(yàn)證或自動(dòng)驗(yàn)證的講解,對(duì)學(xué)習(xí)JavaScript編程技術(shù)有所幫助,感興趣的小伙伴們可以參考一下2015-12-12Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output詳解(下)
這篇文章主要給大家介紹了關(guān)于Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-07-07