Skip to content

Instantly share code, notes, and snippets.

@WillsonSmith
Last active August 29, 2015 14:13
Show Gist options
  • Save WillsonSmith/981d259ca65db22884ec to your computer and use it in GitHub Desktop.
Save WillsonSmith/981d259ca65db22884ec to your computer and use it in GitHub Desktop.
function checkIfNew(tagName, cb) {
"use strict";
var nodeList = document.getElementsByTagName(tagName);
var tempList = [].slice.call(nodeList);
function conditional() {
var tempLength = tempList.length,
nodeLength = nodeList.length,
lastTemp = tempList[tempLength - 1],
lastNode = nodeList[nodeLength - 1],
condition = false;
condition = tempLength === nodeLength ? false : true;
//this check covers if moving item
condition = lastTemp === lastNode ? false : true;
return condition;
}
function compareTemp() {
if (conditional()) { //|| tempList[ tempList.length - 1 ] !== nodeList[ nodeList.length - 1 ]) {
tempList = [].slice.call(nodeList);
//may change to return diffs from a filter
//added or removed
cb(tempList);
}
window.requestAnimationFrame(compareTemp);
}
return {
loopList: function() {
window.requestAnimationFrame(compareTemp);
},
getCurrent: function() {
return tempList;
}
};
}
var x = checkIfNew("div", function(newList) {
console.log(newList);
});
x.loopList();
document.body.appendChild(document.createElement("div"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment