jQuery Deferred Demo without

by Trae

HTML

<H1>jQuery Deferred Demo</H1>
<a href=http://jsfiddle.net/Trae/NGZ7U/ target=blank>Promises Resources</a><br />

JavaScript

wait(50, undefined, function(){
    console.log("Do Something");
    wait(500, function () {
        console.log("checking condition: " + count)
        return (count++ > 5);
    }, function(){
        console.log("Do Something Else");
        wait(50, undefined, function(){
            console.log("Do Something Else Again")
        });
        return "Finshed doing something else";
    });
});

var count = 1;

function wait(interval, condition, success) {
    if (!condition) {
        setTimeout(success, interval);
        return;
    }
    innerWait();
    return;
    
    function innerWait() {
        if (condition()) {
            console.log("success");
            success();
            return false;
        } else {
            setTimeout(innerWait, interval);
        }
    }
}