Last active
March 16, 2016 06:35
-
-
Save bhavyaw/e52f4512e16d1810afa7 to your computer and use it in GitHub Desktop.
Async For Loop
This file contains 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
/** | |
* 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