Yes, it is possible, but you cannot do it simply returning from the function delay()after ms- the setTimeout calls are asynchronous and therefore return immediately. To achieve this effect, you must implement your own queuing system. Some code:
var queue = new Queue();
function delay(ms) {
queue.enqueue("delay");
setTimeout(function() {
popQueue(true);
}, ms);
return this;
}
function say(s) {
queue.enqueue(function() {
alert(s);
});
popQueue();
return this;
}
function popQueue(removeDelay) {
if (removeDelay) {
if (queue.peek() == "delay") queue.dequeue();
}
while (!queue.isEmpty() && queue.peek() != "delay") {
(queue.dequeue())();
}
}
, , , "", , . delay() "", , . .
, , . , - Queue implementation, , SelfExecutingQueue ( , ) , , popQueue() ..