Skip to content

Commit

Permalink
Fix a bug where the callback was invoked for as many times as the fil…
Browse files Browse the repository at this point in the history
…es count and not just once when all are loaded
  • Loading branch information
mbarzeev committed Oct 8, 2012
1 parent f944d77 commit 3d21d65
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions abbey-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@
request.open('GET', file[fileKey], true);
request.responseType = 'arraybuffer';

request.onload = function () {
request.onload = function () {
filesLoaded++;
context.decodeAudioData(request.response, function (decodedBuffer) {
returnObj[fileKey] = decodedBuffer;

if (filesLoaded === numberOfFiles) {
if (Object.size(returnObj) === numberOfFiles) {
callback(returnObj);
}
});
Expand All @@ -40,9 +39,9 @@
var returnObj = {};

files.forEach(function (file, index) {
numberOfFiles = Object.size(file);
for (var key in file) {
if (file.hasOwnProperty(key)) {
numberOfFiles++;
loadFile(key, file, returnObj, callback);
}
}
Expand All @@ -52,3 +51,11 @@

window.AbbeyLoad = AbbeyLoad;
})(window);

Object.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};

0 comments on commit 3d21d65

Please sign in to comment.