I am trying to force a serialized logic onto a set of asynchronous activities on a webpage. I am fairly certain I want to use the jQuery deferred object, but I run into the problem that the functions I want to execute are dependent on when the user decides to make selections by clicking various buttons. I am looking for help doing this using the following jsFiddle idea:
Consider a sequence of 4 buttons. When clicked, each button disables itself and enables the next button. Each button shouldn't have its event set until after it has been enabled. There's an added task (in this case an alert) to be done ONLY AFTER the 3rd button has been enabled.
Basic HTML Code
<button id="btn1">Click 1st</button>
<button id="btn2" disabled="disabled">Click 2nd</button>
<button id="btn3" disabled="disabled">Click 3rd</button>
<button id="btn4" disabled="disabled">Click 4th</button>
Tasks In Each Step
var fnDoStageOne = function() {
$("#btn1").one("click", function(event, ui) {
$("#btn1").prop("disabled", true);
$("#btn2").prop("disabled", false);
//STAGE ONE IS ONLY DONE AFTER THIS POINT
});
};
var fnDoStageTwo = function() {
$("#btn2").one("click", function(event, ui) {
$("#btn2").prop("disabled", true);
$("#btn3").prop("disabled", false);
//STAGE TWO IS ONLY DONE AFTER THIS POINT
});
};
var fnDoStageThree = function() {
$("#btn3").one("click", function(event, ui) {
$("#btn3").prop("disabled", true);
$("#btn4").prop("disabled", false);
//STAGE THREE IS ONLY DONE AFTER THIS POINT
});
alert("Shouldn't see this if button 3 isn't active yet");
};
var fnDoStageFour = function() {
$("#btn4").one("click", function(event, ui) {
$("#btn4").prop("disabled", true);
alert("Task complete");
//STAGE FOUR IS ONLY DONE AFTER THIS POINT
});
};
Incorrect Control Logic
var oDeferredObj = $.Deferred();
oDeferredObj.then(fnDoStageOne);
oDeferredObj.then(fnDoStageTwo);
oDeferredObj.then(fnDoStageThree);
oDeferredObj.then(fnDoStageFour);
oDeferredObj.resolve();
The jsfiddle can be seen here: http://jsfiddle.net/sva79/
My initial understanding was that I could just chain the functions into the deferred with the .then() function. Obviously, this doesn't work as the additional task in step 3 triggers on page load. How would I need to adjust the control or logic of this scenario to put of resolving each step until the appropriate button press has been registered?