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); });
新聞熱點
疑難解答