当前位置: 首页> 健康> 知识 > 温州自媒体公司_北京高端网站建设规划_短链接生成_网络营销专业介绍

温州自媒体公司_北京高端网站建设规划_短链接生成_网络营销专业介绍

时间:2025/7/14 11:51:45来源:https://blog.csdn.net/qq_44476779/article/details/144724252 浏览次数:0次
温州自媒体公司_北京高端网站建设规划_短链接生成_网络营销专业介绍

直接上代码

防抖(js)
export function debounce(fn, time, immediate) {let timer = null;const debounced = function () {const context = this;const args = arguments;if (timer) clearTimeout(timer); // 清除前一个定时器if (immediate) {// 为true立即执行const callNow = !timer;timer = setTimeout(() => {timer = null;}, time || 500);if (callNow) fn.apply(context, args);} else {// 非立即执行timer = setTimeout(function () {fn.apply(context, args);}, time || 500);}};debounced.cancel = function () {// 取消防抖clearTimeout(timer);timer = null;};return debounced;
}
节流(js)
/*** 节流throttle:所谓节流,就是指连续触发事件但是在 n 秒中只执行一次函数。* 节流会稀释函数的执行频率。* @param {Function} func 执行函数* @param {Number} wait 毫秒数* @param {boolean} [immediate=true] 是否立刻执行* @returns*/
function throttle(func, wait, immediate = true) {let prevTime = 0;let first = true;const execu = ({ currentTime, context, args }) => {if (currentTime >= prevTime) {func.apply(context, args);first = true;}};const reset = (currentTime) => {if (first) {prevTime = currentTime + wait;first = false;}}return function () {const context = this;const args = arguments;const currentTime = Date.now();if (immediate) {execu({ currentTime, context, args });reset(currentTime);} else {reset(currentTime);execu({ currentTime, context, args });}}
}
关键字:温州自媒体公司_北京高端网站建设规划_短链接生成_网络营销专业介绍

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: