Skip to content

Instantly share code, notes, and snippets.

// Single for loop (i)
// i loop
loop(0, function(i){return i<10;}, function(i){return i+1;},
function(i, next){
// looping.
console.log(i);
next();
},
function(i){ console.log("all done."); }
);
function loop(init, fnCondition, fnUpdate, fnRun, fnDone){
var index = 0;
index = (init.constructor.name === "function")?init():init;
function next(){
index = fnUpdate(index);
if(fnCondition(index)){
setTimeout(function(){
fnRun(index, next);
}, 0);
}else{
(function(){
var i = 0;
var j = 0;
var k = 0;
var m = 0;
function forloop1(){
if(i<5){
j = 0;
forloop2();
}else{
for(var i=0; i<5; i++){
for(var j=0; j<5; j++){
for(var k=0; k<5; k++){
for(var m=0; m<5; m++){
console.log(i,j,k,m);
}
}
}
}
(function(){
var i = 0;
var j = 0;
function forloop1(){
if(i<10){
j = 0;
forloop2();
}else{
console.log("all done");
}
(function(){
var i = 0;
function forloop1(){
if(i<10){
(function(){
var j = 0;
function forloop2(){
if(j<10){
console.log(i,j);
j++;
for(var i=0; i<10; i++){
for(var j=0; j<10; j++){
console.log(i,j);
}
}
console.log("all done.");
(function(){
var i = 0;
function forloop(){
if(i<5){
$.getJSON('/data'+i+'.json', function(data) {
console.log("yay! Data"+i+" is ready!");
i++;
forloop();
});
}else{
$.getJSON('/data0.json', function(data) {
console.log("yay! Data0 is ready!");
// ok, now use that to get the next piece of data.
$.getJSON('/data1.json', function(data) {
console.log("yay! Data1 is ready!");
// ok, now use that to get the next piece of data.
$.getJSON('/data2.json', function(data) {
console.log("yay! Data2 is ready!");
// ok, now use that to get the next piece of data.
$.getJSON('/data3.json', function(data) {
$.getJSON('/data0.json', function(data) {
console.log("yay! Data0 is ready!");
});