Last active
December 10, 2015 16:18
-
-
Save aaronfrost/4460066 to your computer and use it in GitHub Desktop.
This is the code that is faster than AJ's code
This file contains hidden or 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
var thingsCount = 0; | |
var urls = [] | |
, deferreds = [] | |
; | |
var mainDeferred = $.Deferred(); | |
deferreds.push(mainDeferred); | |
$.get('https://www.lds.org/directory/services/ludrs/unit/current-user-units/',function(r){ | |
JSON.parse(r)[0].wards.forEach(function(ward){ | |
$.get('https://www.lds.org/directory/services/ludrs/mem/wardDirectory/photos/'+ward.wardUnitNo,function(r){ | |
var hh = JSON.parse(r); | |
var wardThings = []; | |
//console.log(hh); | |
hh.forEach(function(household){ | |
if(household.photoUrl.length){ | |
wardThings.push({ | |
ward:ward, | |
household : household | |
}); | |
} | |
}); | |
var img; | |
thingsCount += wardThings.length; | |
//console.log() | |
wardThings.forEach(function(thing){ | |
//console.log('hi'); | |
img = document.createElement('img'); | |
img.onload = function(){ | |
var c = document.createElement('canvas'); | |
var x = c.getContext('2d'); | |
c.height = this.height, | |
c.width = this.width; | |
x.drawImage(this, 0,0); | |
urls.push({ | |
name : thing.household.householdName, | |
imageData : c.toDataURL('image/jpeg', 0.2) | |
}); | |
if(thingsCount == urls.length){ | |
mainDeferred.resolve(); | |
} | |
}; | |
img.src = thing.household.photoUrl; | |
}); | |
}); | |
}); | |
}); | |
$.when.apply(null, deferreds) | |
.then(function(){ | |
$.ajax({ | |
type:'post', | |
processData:false, | |
data: JSON.stringify(urls), | |
url:'TO BE DETERMINED', | |
success: function(r){ | |
console.log("SERVER SUCCESS RESPONSE:", r); | |
}, | |
error:function(){ | |
console.log("SERVER ERROR RESPONSE") | |
} | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment