Javascript on window resize end

前端 未结 3 509
谎友^
谎友^ 2021-01-12 03:05

I am calling a function when the window is resized like this:

window.addEventListener(\"resize\", calculateDimensions());

But I need a way

3条回答
  •  自闭症患者
    2021-01-12 03:39

    You could set a Timeout and reset it when resize is fired again. So the last timeout isnt canceled and the function is run:

    function debounce(func){
      var timer;
      return function(event){
        if(timer) clearTimeout(timer);
        timer = setTimeout(func,100,event);
      };
    }
    

    Usable like this:

    window.addEventListener("resize",debounce(function(e){
      alert("end of resizing");
    }));
    

提交回复
热议问题