最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Angular實現(xiàn)防抖和節(jié)流的示例代碼

 更新時間:2024年02月20日 09:10:53   作者:crary,記憶  
這篇博客主要是詳細介紹兩種常用Angular實現(xiàn)防抖和節(jié)流的方法:使用RxJS操作符和使用Angular自帶的工具,文中通過代碼示例給大家講解的非常詳細,需要的朋友可以參考下

在Angular中實現(xiàn)防抖和節(jié)流的方法有多種,這篇博客主要是詳細介紹兩種常用的方法:使用RxJS操作符和使用Angular自帶的工具。

  • 使用RxJS操作符實現(xiàn)防抖和節(jié)流:

     防抖(Debounce):

//簡易版
import { debounceTime } from 'rxjs/operators';
input.valueChanges.pipe(
  debounceTime(300)
).subscribe(value => {
  // 執(zhí)行搜索操作
});
 
 
//詳細版
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í)行滾動操作
});
 
//詳細版
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自帶的工具實現(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實現(xiàn)防抖和節(jié)流的示例代碼的詳細內容,更多關于Angular防抖和節(jié)流的資料請關注腳本之家其它相關文章!

相關文章

最新評論

灵武市| 进贤县| 汤阴县| 中江县| 西充县| 车致| 七台河市| 彭州市| 合川市| 长岛县| 淅川县| 罗山县| 瓦房店市| 星座| 红河县| 南京市| 临江市| 唐山市| 临武县| 鄂托克前旗| 淅川县| 丁青县| 连城县| 隆安县| 南开区| 鹿邑县| 新密市| 望城县| 中阳县| 滨海县| 永州市| 克拉玛依市| 大厂| 镇原县| 定州市| 湖南省| 甘孜| 九江市| 绥阳县| 韶山市| 怀来县|