UNPKG

superyoung-js

Version:

工具集

50 lines (48 loc) 1.37 kB
/* * @Description: 工具类集合 * @Author: SuperYoung * @Date: 2019-12-04 23:11:56 * @LastEditors: SuperYoung * @LastEditTime: 2019-12-04 23:21:00 */ /** * @description: 节流方法/连续触发事件但是在 n 秒中只执行一次 * @author: SuperYoung * @Date: 2019-12-04 23:12:27 * @lastEditors: SuperYoung * @param {*} fun 执行函数 * @param {*} time 节流时间,毫秒 * @return: */ export const fnJieliu = function (fun, time) { let timer = null return function () { if (!timer) { timer = setTimeout(() => { fun.apply(this, arguments) // 或者直接 fun() timer = null }, time) } } } /** * @description: 防抖方法/触发事件后在 n 秒内函数只能执行一次 * @author: SuperYoung * @Date: 2019-12-04 23:16:22 * @lastEditors: SuperYoung * @param {*} fun 执行函数 * @param {*} time 防抖时间,毫秒 * @return: */ export const fnFangdou = function (fun, time) { let timer = null return function () { if (timer !== null) clearTimeout(timer) // 如果多次触发将上次记录延迟清除掉 timer = setTimeout(() => { fun.apply(this, arguments) // 或者直接 fun() timer = null }, time) } }