If you’re used to something like jQuery UI resizeable, you get events you can bind to during the resizing, but also at the end of resizing.
No such event exists in native JavaScript.
You can fake it by setting a timeout to run the code you want to run when resizing stops. Then clear that timeout every time a resize event fires. That way the timeout will only finish if that timeout actually finishes.
var resizeTimer;
$(window).on('resize', function(e) {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function() {
// Run code here, resizing has "stopped"
}, 250);
});
Similar to debouncing.
Yeah !
I know about this fallback in resize window.
Very appreciated your trick !
This technique is called “debouncing”. If you happen to be using underscore, the code is quite simple:
David Walsh wrote up a reasonable overview of how this works.
I mentioned that it’s similar to debouncing at the end of the article. But it’s not quite the same. Debouncing will run it every (whatever) seconds no matter what, slightly different than “wait until you’re done”.
You’re thinking of throttling, which will call the function once every (whatever) seconds. Debouncing will either run once at the beginning or end of the series of calls, so long as they happen within (whatever) seconds, but you already knew that.
Hi Chris
I use a trick like this at work with one small difference: instead of executing code inline, we fire a second event (I call mine ‘calmresize’). This way, we attach just one resize handler across the site and modules can listen to the calmresize event instead.
The code:
Hope this helps someone. Thanks for all the code snippets Chris – you’ve dug me out many a hole!
Sorry, should be $(window)
Hey Matt,
I like your snippet and would like to integrate it in a jQuery plugin. Is that OK for you and how should i credit?
Regards,
Sebastian
What is the purpose of using prototype here?
Hey Sebastian, glad you found it useful. If you could credit to Matt Perry and PaperShaker uk.paper-shaker.com that’d be excellent.
Thanks
Matt
i think i found a quite simple solution. it also works for scroll-events, …
This is useful, if you want to trigger the same resize-function after resizing (if you resize very quickly sometimes values don’t update)
I’ve published a resizeend event emitter on NPM:
https://www.npmjs.com/package/resize-end
I’m new to web page development and I’m having some trouble with some code I’m working on. I need to change a CSS property (I think that’s what it’s called) on the end of a window resize (or I could do it at the beginning I guess). It’s the left property. I have a class called sections and I need to set left: to 0;. I’ve been trying code like this and haven’t had much look with it working. I’ll try this come tomorrow, but I have a quick question. Do I need anything for this code to work? Or do I just have to include jQuery? The other examples I’ve seen just don’t seem to work at all. And I know you said these functions don’t exist in native Javascript but you can fake it using your code…is your code jQuery or is it Javascript? I was under the impression they’re two different things. I thought jQuery was written in Javascript but wasn’t in fact Javascript. It was something different. I’m just a bit confused.
Just wanted to thank you for writing a great article. I have successfully incorporated it into my website and it works now. Thank you.
Nice one, it saved my productivity hours…
For css3 Transition
$(‘.target’).on(‘webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend’, _.debounce(function () {
console.log(‘Your Code here’)
}, 250));
The best solution is here:
http://stackoverflow.com/questions/5489946/jquery-how-to-wait-for-the-end-of-resize-event-and-only-then-perform-an-ac/27923937#27923937
it’s very elegant and effective. Just wonderful
Thanks for this!
Works very well! Thank you!
I noticed that this method isn’t always reliable in terms of ‘catching’ the end of a user’s resizing. I have a method that uses the viewport width in order to add or take away columns from a feed of items, and I find that by using this method to debounce it, it sometimes doesn’t ‘catch’ the final viewport width when the user has finished resizing the screen. Not sure if this has to do with the timeout negating the final window state but it seems to not be technically reliable.
Chris, I just worked on a site that I couldn’t use lodash on, just jQuery and this is perfect and simple. You’ve thought through so many awesome useful things on the good ol’ CSS-Tricks. I can always trust you!