Try # 1 : here is a solution that does not require additional global variables:
request1.onComplete = function() {
The handler for each event fires the first one, clears the other old handler and assigns a new one that includes the material that you need to do after both events are completed. Since we re-assign the second handler inside the handler of the first event (depending on what it is), we always know that we finished when the second handler ends.
Try # 2 . Here, something will work if each type of event is different:
function onBoth(fn) { var last, done = false; return function(e) { if (last && last !== e.type && !done) { fn();
For example, this will not warn βdoneβ until the user scrolls and clicks:
var both = onBoth(function() { alert("done") }); document.addEventListener("scroll", both, false); document.addEventListener("click", both, false);
Try # 3 . The previous attempt can be modified to work with similar events:
function onBoth(fn) { var last, done = false; return function(curr) { if (last && last !== curr && !done) { fn();
... which should be used as follows:
var check = onBoth(function() { alert("done") }); request1.onComplete = function() { check(arguments.callee); } request2.onComplete = function() { check(arguments.callee); }
Basically, this checks the execution of two different callbacks, keeping a reference to the last callback executed. Its use is a bit awkward, but it does its job (i.e., it will work if each of the events is executed more than once).
Wayne burkett
source share