, #notifyState setTimeout . setTimeout, :
var notifyState = function(state) {
}
notifyState('State 1');
notifyState('State 2');
setTimeout , , :
var notifyState = function(state, callback) {
setTimeout(function() {
if (callback) {
callback();
}
}, 2000);
}
notifyState('State 1', function() {
notifyState('State 2');
});
, . , #notifyState Flash, , , notifyState , 2 parallels StateState. , notifyState, . , setTimeout . . :
var Queue = [],
running = false;
var notifyState = function(state) {
if (running) {
Queue.push(state);
} else {
running = true;
setTimeout(function() {
running = false;
var nextState = Queue.pop();
if (nextState) {
notifyState(nextState);
}
}, 2000);
}
}