Skip to content

Instantly share code, notes, and snippets.

@eternal44
Created December 22, 2015 04:27
Show Gist options
  • Save eternal44/19d2a013f2949cb09750 to your computer and use it in GitHub Desktop.
Save eternal44/19d2a013f2949cb09750 to your computer and use it in GitHub Desktop.
Trouble shooting infinite loop
var getElementsByClassName = function(className){
// Array of document element
var elementList = Array.prototype.slice.call(
document.body.querySelectorAll('*')
);
var results = [];
var count = elementList.length;
// check body independently since the above array doesn't contain 'body'
if (document.body.className === className) results.push(document.body);
// why doesn't the recursion trigger this countdown?
count -= 1;
if (count === 0) {
return results;
} else {
if(elementList[count].className === className){
results.push(elementList[count]);
}
getElementsByClassName(className);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment