MDUI文件
複製 llms.txt 連結複製 llms-full.txt 連結以 Markdown 格式檢視此頁與 ChatGPT 討論此頁內容與 ChatGPT 討論專案完整文件
預設顏色
自選顏色
從桌布擷取顏色
請選擇一張桌布
開發指南
AI 輔助開發
樣式
與框架整合
元件
函式
jq 工具庫 dialog alert confirm prompt snackbar getTheme setTheme getColorFromImage setColorScheme removeColorScheme loadLocale setLocale getLocale throttle observeResize breakpoint
獨立程式庫

throttle

throttle 函式用來建立一個節流函式,該函式在指定的時間間隔內最多只執行一次。

使用方式

按需引入函式:

import { throttle } from 'mdui/functions/throttle.js';

使用範例:

// 這個函式在 100 毫秒內最多執行一次,可避免在捲動時過於頻繁地呼叫該函式
window.addEventListener(
  'scroll',
  throttle(() => {
    console.log('update');
  }, 100),
);

API

throttle(func: Function, wait: number): Function

函式的第一個參數是需要進行節流操作的函式,第二個參數是指定的時間間隔(單位:毫秒)。函式的回傳值是經過節流處理的函式。

本頁目錄