Skip to content

引入

ts
import { throttle } from 't-comm';

// 不支持 tree-shaking 的项目
import { throttle} from 't-comm/lib/throttle/index';

// 只支持 ESM 的项目
import { throttle} from 't-comm/es/throttle/index';

throttle(fn, time)

描述

节流

连续触发事件但是在 n 秒中只执行一次函数

参数

参数名类型描述
fnfunction

主函数

timenumber

间隔时间,单位 ms

返回:

闭包函数

示例

ts
function count() {
 console.log('xxxxx')
}
window.onscroll = throttle(count, 500)