javascript 节流调整大小触发的事件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 节流调整大小触发的事件相关的知识,希望对你有一定的参考价值。

/*
 * Throttle Resize-triggered Events
 * Wrap your actions in this function to throttle the frequency of firing them off, for better performance, esp. on mobile.
 * ( source: http://stackoverflow.com/questions/2854407/javascript-jquery-window-resize-how-to-fire-after-the-resize-is-completed )
*/
var waitForFinalEvent = (function () {
	var timers = {};
	return function (callback, ms, uniqueId) {
		if (!uniqueId) { uniqueId = "Don't call this twice without a uniqueId"; }
		if (timers[uniqueId]) { clearTimeout (timers[uniqueId]); }
		timers[uniqueId] = setTimeout(callback, ms);
	};
})();

// how long to wait before deciding the resize has stopped, in ms. Around 50-100 should work ok.
var timeToWaitForLast = 100;

$(window).resize(function () {
    waitForFinalEvent( function() {
        //FIRE SCRIPTS HERE
    }, timeToWaitForLast, "your-function-identifier-string"); 
 });

以上是关于javascript 节流调整大小触发的事件的主要内容,如果未能解决你的问题,请参考以下文章

节流和去抖动功能

函数防抖与函数节流

检测何时使用 JavaScript 调整窗口大小?

iphone/ipad 触发意外的调整大小事件

VUE中的函数的防抖和节流 以及应用场景

JS中的函数节流