本文介绍了JavaScript onresize事件多次触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在触发onresize事件时尝试只运行一次函数时遇到了一些麻烦,我已经看过这些问题但我没有使用jQuery,并且无法使用该功能。有什么建议?
I'm having some trouble trying running a function only once when onresize event is triggered, I have looked at this questions DOM onresize event but I'm not using jQuery and can't get that function to work without. Any suggestions?
推荐答案
在调整大小操作期间,每个帧都会调用一次resize事件。如果要在调整大小时调用它,只需使用回调函数设置计时器。
The resize event is called once for every frame during the resizing operation. If you want to call it when resizing is finished, just set timer with a callback function.
var timeOut = null;
window.onresize = function(){
if (timeOut != null)
clearTimeout(timeOut);
timeOut = setTimeout(function(){
// YOUR RESIZE CODE HERE
}, 500);
};
这篇关于JavaScript onresize事件多次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!