Hi, I'm Chris Moyer's Blog.

jThrottle - A jQuery Plugin to Throttle Big Loops

Uh oh!

Inspired by Oliver Steele's Sequentially Library and this post by Felix Geisendörfer, I created a very simple jQuery plugin.

It provides a drop-in replacement for each(), which performs the loop in iterations and pauses between each iteration. You'd only do this if you are iterating over a largish set and performing non-trivial operations. Basically, if the browser freezes, this is a decent solution as the call to setTimeout() allows the browsers UI thread to get a slice of the action and prevents spinning cursor syndrome.

If you wrote:

$('foo').each(function () { alert($(this).html()); });
Now you write:


$('foo').throttle(function () { alert($(this).html()); });

Or:

$('foo').throttle(

		function () { alert($(this).html()); },

		{per: 100, pause: 10});


Back to Blog Home »