/* jQuery processEach plugin * Version processEach * 10.05.2009 * URL: http://borgar.net/svn/public/trunk/javascript/jquery.processeach/ * Description: A variation of the code each function for enormous or labour intensive loops where the thread is yelded every iteration to prevent script timeouts. * Author: Borgar Þorsteinsson * Copyright: Copyright (c) 2009 Borgar Þorsteinsson under dual MIT/GPL license. *//*global jQuery */ jQuery.processEach = function ( obj, callback, done ) { var f, l, r, k, i = 0, m; if ( obj && typeof obj === 'object' ) { // for normal arrays, or jQuery objects, trust the length if ( $.isArray( obj ) || obj.jquery ) { l = obj.length; } // copy regular objects into an array else { m = []; for ( k in obj ) { m[ m.length ] = k; } l = m.length; } } (f = function () { if ( i < l ) { k = m ? m[ i ] : i; r = callback.call( obj[ k ], k, obj[ k ] ); // break process with false if ( r !== false ) { i++; setTimeout( f, 1 ); } else if ( $.isFunction( done ) ) { done.call( obj, r ); } } else if ( $.isFunction( done ) ) { done.call( obj, r ); } })(); };