函数防抖

    xiaoxiao2021-03-26  28

    1.通过设置延时和取消延时来保证函数在事件触发一段时间后执行

    $(document).ready(function() { function bebounce(fn, delay) { var timer = null; return function() { var context = this; var arg = arguments; //每次一调用函数时,取消上一次的定时 clearTimeout(timer); timer = setTimeout(function() { fn.apply(context, arg) }, delay); } } function showWidth() { console.log(window.innerWidth); } window.addEventListener('resize', bebounce(showWidth, 1000), false); });

    转载请注明原文地址: https://ju.6miu.com/read-663160.html

    最新回复(0)