• Stars
    star
    787
  • Rank 57,828 (Top 2 %)
  • Language
    JavaScript
  • Created about 15 years ago
  • Updated over 12 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Debounced and Throttled Resize Events for jQuery

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é.

More Repositories

1

background-size-polyfill

Adds support for background-size "cover" and "contain" to IE8
JavaScript
1,264
star
2

jquery.transform.js

jQuery cssHooks adding a cross browser transform property to $.fn.css() and $.fn.animate()
JavaScript
620
star
3

jquery.transition.js

Upgrade jQuery's .animate() method to use CSS3 Transitions in modern browsers.
JavaScript
437
star
4

jquery.backgroundSize.js

DEPRECATED, please use its successor: background-size polyfill
JavaScript
263
star
5

Activable

declarative UI components | 2K, 0 dependency, IE8 compat.
JavaScript
82
star
6

menu-effects

CSS3 menu effects
JavaScript
45
star
7

rotor.js

rotate3d manipulation library - lightweight and extensible
JavaScript
43
star
8

jquery.persival.js

Persival can persist the values users entered in forms, to make them resilient to "inadvertent tab closing" and crashes
JavaScript
35
star
9

node-DJs

Restart a server after each change in main script and dependencies
JavaScript
30
star
10

gulp-shelter

A cozy place for your shell tasks
JavaScript
22
star
11

borderimage

cross-browser implementation of border-image CSS3 property
JavaScript
21
star
12

mediaCache

Put image, audio and video resources in a dynamic cache for offline use
PHP
17
star
13

touch-devtool

Simulate touch events with a mouse, for fun and profit.
JavaScript
16
star
14

node.s3.js

A high level, file oriented javascript API to Amazon S3
JavaScript
10
star
15

jquery.browser

Browser and mobile platform sniffing scripts (plugin & standalone)
JavaScript
9
star
16

WordPress-Post-Relations

Lightweight relations between WordPress post types
PHP
8
star
17

jquery.backgroundXY.js

A jQuery cssHooks implementing background-position-X and background-position-Y in browsers lacking it.
JavaScript
8
star
18

orientation-devtool

Simulate orientation events, for fun and profit
JavaScript
8
star
19

victor

Convert your SVG files to lightweight, Raphael-compatible JSON
JavaScript
7
star
20

WordPress-Sortable-Posts

Reorder posts by drag-and-drop
JavaScript
7
star
21

geolocation.js

a polyfill providing a normalized W3C geolocation API across browsers.
JavaScript
7
star
22

store.space.js

a store.js plugin bringing namespaces to localStorage, in half a Kb
JavaScript
6
star
23

jquery.cookie

The ultimate library to write cookies with jQuery
6
star
24

node.pdf.js

Pdf generation for node using wkhtmltopdf
JavaScript
6
star
25

Math.uuid.js

Minimalistic version of Robert Kieffer's Math.uuid.js, primarily aimed at interacting with couchDB
JavaScript
5
star
26

froglet.js

sandboxed-Widget boilerplate for bookmarklet and extension
JavaScript
4
star
27

slack-app-in-tab

A Chrome + Firefox extension to open the Slack app with its multi-workspace sidebar in a simple browser tab
JavaScript
4
star
28

fxngn.js

A minimalist effects/game engine built to evaluate the influence of != parameters on performances
JavaScript
4
star
29

node.csv.js

Every language need its CSV parser, JavaScript needs a JSON style API
JavaScript
4
star
30

todoso

ToDoSo is a presentation authoring, playing and sharing Web application
JavaScript
4
star
31

ti

nothing interesting
JavaScript
3
star
32

jquery-interval-bookmarklet

Experimenting with visual performances of jQuery animations
JavaScript
3
star
33

jquery.delegate.js

Backport the delegate function introduced in jQuery 1.4, for jQuery 1.3
JavaScript
3
star
34

jquery.updateInterval

update $.fx.interval property dynamically and don't wait to see the effect
JavaScript
3
star
35

designer-tools

cross-browser plugin that makes it easy to style your pages in a cross-browser way
JavaScript
3
star
36

EventSound

audible jQuery events
JavaScript
2
star
37

CV

Take a wild guess
1
star
38

xentity

Declarative UI Component Factory
JavaScript
1
star
39

Slasher

slasher game for the GDPJAM
JavaScript
1
star
40

todosOverlay

The overlay plugin for jQuery that is used on the ToDoSo introduction page
1
star