npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jquery-debounced-and-throttled-resize

v1.0.1

Published

Debounced and Throttled Resize Events for jQuery

Downloads

351

Readme

Debounced and Throttled Resize Events for jQuery

It has always been a pain to deal with cross browser issues of the window's resize event:

  • in IE, many resize events fire as long as the user continues resizing the window,
  • Chrome an Safari behave like IE, but resize events always fire two by two,
  • Firefox used to fire one resize event at the end of the resizing, but now behaves like IE,
  • Opera behaves like IE, but fires resize events at a reduced rate.

This project offers two scripts, each providing a special jQuery event that make resize more manageable:

  • jquery.debouncedresize.js: adds a special event that fires once after the window has been resized,
  • jquery.throttledresize.js: adds a special event that fires at a reduced rate (no more double events from Chrome and Safari).

The Demo should help you make your choice.

Note to previous users: jquery.debouncedresize.js is the equivalent of the old jquery.smartresize.js, only the name of the special event changes. Update is not required unless you want to add jquery.throttledresize.js to a page page that already has jquery.smartresize.js.

Binding / Unbinding

Simply bind your special event just like a normal resize event.

$(window).on("debouncedresize", function( event ) {
	// Your event handler code goes here.
});

// or...
$(window).on("throttledresize", function( event ) {
	// Your event handler code goes here.
});

// unbind at will
$(window).off( "debouncedresize" );

Threshold

Both special events have a .threshold option:

  • in jquery.debouncedresize.js, it defines the interval used to determine if two resize events are part of the same debouncedresize event. Defaults to 150 (milliseconds)
  • in jquery.throttledresize.js, it defines the number of animation ticks (or frames) between each throttledresize event. Defaults to 0 (tick), which means that it's going to fire at a maximum of 60fps.

They can be modified globally once the script has been loaded:

// increase the threshold to 250ms
$.event.special.debouncedresize.threshold = 250;

// decrease the firing rate to a maximum of 30fps
$.event.special.throttledresize.threshold = 1;
// 2 <=> 20fps, 3 <=> 15fps, ...

(Synchronous) Trigger

Triggering those events is achieved using jQuery's standard API:

$(window).trigger( "debouncedresize" );

It's also possible to execute the handler of any listener synchronously (without the delays):

$(window).trigger( "throttledresize", [true] );

Minimalist Standalone Version

Most of the time, I find myself using debouncedresize just to register a single listener on window. As it turns out, all the features I need actually fit in 91 bytes:

// debulked onresize handler
function on_resize(c,t){onresize=function(){clearTimeout(t);t=setTimeout(c,100)};return c};

Using it is pretty simple:

on_resize(function() {
  // handle the resize event here
  ...
});

Initializing a page (by executing the resize handler when the page loads) couldn't be easier:

on_resize(function() {
  ...
})(); // these parenthesis does the trick

No files are provided for this function, simply copy/paste it from this README.

License

MIT licensed http://louisremi.mit-license.org/

Copyright (c) 2012 Louis-Rémi Babé.