Permalink
Browse files

Fix a bug where the callback was invoked for as many times as the fil…

…es count and not just once when all are loaded
  • Loading branch information...
1 parent f944d77 commit 3d21d655df83ada811c3f70ff01b9986f8fa0cef @mbarzeev mbarzeev committed Oct 8, 2012
Showing with 11 additions and 4 deletions.
  1. +11 −4 abbey-load.js
View
@@ -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);
}
});
@@ -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);
}
}
@@ -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.