Angular實(shí)現(xiàn)防抖和節(jié)流的示例代碼
在Angular中實(shí)現(xiàn)防抖和節(jié)流的方法有多種,這篇博客主要是詳細(xì)介紹兩種常用的方法:使用RxJS操作符和使用Angular自帶的工具。
- 使用RxJS操作符實(shí)現(xiàn)防抖和節(jié)流:
防抖(Debounce):
//簡易版
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):
//簡易版
import { throttleTime } from 'rxjs/operators';
scrollEvent.pipe(
throttleTime(300)
).subscribe(() => {
// 執(zhí)行滾動操作
});
//詳細(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í)行滾動操作
});
}
}- 使用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í)行滾動操作
}, 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é)流的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
angular $watch 一個(gè)變量的變化(實(shí)例講解)
下面小編就為大家?guī)硪黄猘ngular $watch 一個(gè)變量的變化(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
Angular 4.x+Ionic3踩坑之Ionic3.x pop反向傳值詳解
這篇文章主要給大家介紹了關(guān)于Angular 4.x+Ionic3踩坑之Ionic3.x pop反向傳值的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03
AngularJS雙向綁定和依賴反轉(zhuǎn)實(shí)例詳解
這篇文章主要介紹了AngularJS雙向綁定和依賴反轉(zhuǎn)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
AngularJs實(shí)現(xiàn)ng1.3+表單驗(yàn)證
這篇文章主要介紹了AngularJs實(shí)現(xiàn)ng1.3+表單驗(yàn)證,感興趣的小伙伴們可以參考一下2015-12-12
angular6.0開發(fā)教程之如何安裝angular6.0框架
這篇文章主要介紹了angular6.0開發(fā)教程之如何安裝angular6.0框架,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
angularjs數(shù)組判斷是否含有某個(gè)元素的實(shí)例
下面小編就為大家分享一篇angularjs數(shù)組判斷是否含有某個(gè)元素的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
發(fā)布Angular應(yīng)用至生產(chǎn)環(huán)境的方法
這篇文章主要介紹了發(fā)布Angular應(yīng)用至生產(chǎn)環(huán)境的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
AngularJS實(shí)現(xiàn)給動態(tài)生成的元素綁定事件的方法
這篇文章主要介紹了AngularJS實(shí)現(xiàn)給動態(tài)生成的元素綁定事件的方法,結(jié)合實(shí)例形式分析了AngularJS動態(tài)生成元素與事件綁定相關(guān)操作技巧,需要的朋友可以參考下2016-12-12

