是否可以在调整窗口大小后仅运行一个函数5次?当您再次调整大小时,再次运行该函数5次。
$(window).bind('resize', function() {
setTimeout(function(){
var slider = $('.wrapper .flexslider').data('flexslider');
slider.resize();
}, 500);
});此函数每0.5秒运行一次。但是浏览器不能处理它,并且运行这个函数一次并不足以正确地调整flexslider的大小(我不知道为什么)。
发布于 2016-02-12 21:23:46
是。向您的函数添加一个回调,并根据需要多次递归地再次调用它。
发布于 2016-02-26 05:01:27
下面是注释中提到的去反跳函数的一个示例。
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
var myEfficientFn = debounce( function() {
// Your resize code here, only fired once
}, 500);
window.addEventListener('resize', myEfficientFn);https://stackoverflow.com/questions/35363697
复制相似问题