Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Lodash Debounce on resize

Tags:

lodash

I'm trying to trigger an event on resize of window, it seems like it does not work.

$(window).resize(function(){
   _.debounce(function(){
       console.log("hello");
   }, 100);
})
like image 510
Vinny Avatar asked Dec 08 '22 15:12

Vinny


2 Answers

It's actually shown in their lodash documentation as an example, lodash#debounce:

$(window).on('resize', _.debounce(function() {
  console.log('resized!');
}, 100));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
like image 192
ryeballar Avatar answered Jan 18 '23 23:01

ryeballar


Vanilla JS in case:

window.onresize = _.debounce(() => {
  console.log('resized!')
}, 100)

EDIT: Actually probably better to use event listener instead:

window.addEventListener('resize', _.debounce(() => {
  console.log('resized!')
}, 100)
like image 43
waffl Avatar answered Jan 19 '23 01:01

waffl