Skip to content

Instantly share code, notes, and snippets.

@federicobucchi
Last active August 29, 2015 14:10
Show Gist options
  • Save federicobucchi/fca81592ff7c9f46c08d to your computer and use it in GitHub Desktop.
Save federicobucchi/fca81592ff7c9f46c08d to your computer and use it in GitHub Desktop.
var arr = ['f', 'e', 'd']
var lazy = (function(){
var counter = 0;
return function(){
console.log(arr[counter]);
counter += 1;
};
})();
lazy();
lazy();
lazy();
var arr = ['f', 'e', 'd'];
var lazy = (function(){
var counter = 0;
var myInterval;
return myInterval = setInterval(function(){
console.log(arr[counter]);
counter += 1;
if (counter >= arr.length) {
clearInterval(myInterval);
}
}, 1000);
})();
var arr = ['f', 'e', 'd'];
for (var i = 0; i < arr.length; i++) {
setTimeout(function(i) {
return function() {
console.log(arr[i]);
}
}(i), 1000 * i);
}
var lazy = (function() {
var cache = [];
var found;
return function(name){
found = cache.filter(function(val) {
return val.name == name;
});
if (found.length == 0){
var pushed = cache.push({
'name': name,
'counter': 0
});
found = cache[pushed - 1];
} else {
found = found[0];
}
console.log(found.name[found.counter]);
found.counter += 1;
if (!found.name[found.counter]) {
found.counter = 0;
}
}
})();
lazy('fed');
lazy('eri');
lazy('eri');
lazy('eri');
lazy('eri');
lazy('fed');
lazy('fed');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment