Last active
February 11, 2024 20:37
-
-
Save dutchcelt/6461825 to your computer and use it in GitHub Desktop.
Array Shifting (recursion)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// When you have a huge amount of DOM elements! | |
// Using an array instead of a loop by 'shifting' the array speeds up the iterations | |
// and setTimeout prevents the browser from locking up. | |
;(function(){ | |
var customScripts = customScripts || {} | |
customScripts.selectorAllToArray = function( selector ){ | |
// Put all the DOM elements in to an array | |
var nodes = document.querySelectorAll( selector ); | |
var elementArray = []; | |
var domElem = null; | |
// Convert the nodelist to an array | |
for ( var i = 0, l = nodes.length; i < l; i++ ) { | |
elementArray.push( nodes[i] ); | |
}; | |
return elementArray; | |
}; | |
})(); | |
;(function(){ | |
var customScripts = customScripts || {} | |
var elementArray = customScripts.selectorAllToArray( "a[name" ); | |
customScripts.iterator = function(){ | |
var start = +new Date(); | |
do { | |
domElem = elementArray.shift(); | |
// Put your DOM elemtent abuse here | |
} while ( elementArray.length > 0 && ( +new Date() - start < 50 ) ); // increase to 100ms if needed. | |
if ( elementArray[0] !== void 0 ){ | |
setTimeout( iterator, 25 ); | |
}; | |
}; | |
customScripts.iterator(); | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment