Skip to content

Instantly share code, notes, and snippets.

@bhavyaw
Last active March 16, 2016 06:35
Show Gist options
  • Save bhavyaw/e52f4512e16d1810afa7 to your computer and use it in GitHub Desktop.
Save bhavyaw/e52f4512e16d1810afa7 to your computer and use it in GitHub Desktop.
Async For Loop
/**
* Class
*/
var AsyncLoop = function(list,fn,customDelay){
var listLength = list.length;
var i = 0;
var currentListItem = null;
var asyncDelay = customDelay || 0;
var self = this;
this.startLoop = function(){
currentListItem = list[i];
var looper = self.startLoop;
if(i < listLength){
setTimeout(function(){
fn(currentListItem,i);
i++;
looper();
},asyncDelay);
}
};
};
/**
* Usage
*/
var asyncLoop = new AsyncLoop(nodes,function(currentNode,i){
currentNode[ currentNode.tagName !== 'LINK' ? 'src' : 'href' ] = currentNode.getAttribute('data-async-load');
currentNode.removeAttribute('data-async-load');
});
asyncLoop.startLoop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment