load scripts in order example - part II wip

by Gerald Gillespie

HTML

<div id="foo">
    <ul id="wtf"></ul></div>

JavaScript

// This code will be loaded as a separate file
var fcode = function(i) {
     return code = //"var d=new Date();$('#wtf').append('* "+i+": <li>'+d+'</li>');";
    "var%20d%3Dnew%20Date()%3B%24('%23wtf').append('*%20" + i + "%3A%20%3Cli%3E'%2Bd%2B'%3C%2Fli%3E')%3B"; //encoded code

}

var weirdfn = function() {
       // n list of actions
    var scripts = ["/echo/js/?delay=1&js=" + fcode('A1'),
                   "/echo/js/?delay=1&js=" + fcode('A2'),
                   "/echo/js/?delay=1&js=" + fcode('A3')];
    var deferred = new $.Deferred(),
        pipe = deferred;
    
    $.each(scripts, function(i, val) {
        pipe = pipe.pipe(function(result) {
            console.log('result ' + i + ': ', result); // "result" is what gets returned thru the pipe from previous call
            return $.getScript(val, function() {
                console.log('loaded ' + val);
            }); // end getscript
        }); // end pipe
    }); // end each : build the pipes from the array of scripts
   
    return deferred;// return promise

};

$.getScript("/echo/js/?delay=3&js=" + fcode('B1'))
    .pipe(function(){
        weirdfn().resolve()
     });

var sT = 
setTimeout(function(){
    $('#wtf').append('some other stuff');
    st();
},500);