Created
January 12, 2012 16:54
-
-
Save jdaigle/1601581 to your computer and use it in GitHub Desktop.
jquery async callback manager
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
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> | |
<script type="text/javascript"> | |
if (window.myCode == undefined) window.myCode = function () { } | |
myCode.asyncManager = function () { | |
} | |
myCode.asyncManager.prototype = { | |
waitingOn: 0, | |
waitingFor: new Array(), | |
waitFor: function (cont) { | |
this.waitingOn++; | |
this.waitingFor.push(cont); | |
}, | |
signalDone: function () { | |
if (this.waitingOn > 0) { | |
this.waitingOn--; | |
} | |
if (this.waitingOn == 0) { | |
this.whenAllDoneCallBack(); | |
} | |
}, | |
whenAllDoneCallBack: function () { }, | |
whenAllDone: function (callback) { | |
this.whenAllDoneCallBack = callback; | |
for (var i in this.waitingFor) { | |
this.waitingFor[i](); | |
} | |
} | |
} | |
$(function () { | |
var m = new myCode.asyncManager(); | |
m.waitFor(function () { | |
$.ajax({ | |
url: '', | |
success: m.signalDone() | |
}); | |
}); | |
m.waitFor(function () { | |
$.ajax({ | |
url: '', | |
success: m.signalDone() | |
}); | |
}); | |
m.whenAllDone(function () { | |
alert("i will display when all callbacks are done"); | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment