DOM onresize event

前端 未结 7 573
无人共我
无人共我 2020-12-02 19:06

If I have this

window.onresize = function() {
  alert(\'resized!!\');
};

My function gets fired multiple times throughout the res

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 19:29

    I always use this when I want to do something after resizing. The calls to setTimeout and clearTimeout are not of any noticable impact on the speed of the resizing, so it's not a problem that these are called multiple times.

    var timeOut = null;
    var func = function() { /* snip, onresize code here */};
    window.onresize = function(){
       if(timeOut != null) clearTimeout(timeOut);
       timeOut = setTimeout(func, 100);
    }
    

提交回复
热议问题