I want use function from path /js/testscript.js, /js/testscript.js is dependent from /script5.js, but testscript.js load after call $(this).testscript();
What am I doing wrong? Scripts are dependent.
$.when
(
$.getScript('/script.js').done(function() {
$.getScript('/script2.js'),
$.getScript('script3.js').done(function() {
$.getScript('/script4.js').done(function() {
$.getScript('/script5.js').done(function() {
$.getScript( "/js/testscript.js" ).done(function() {
console.log("LOADED 2");
})
})
})
})
}),
$.Deferred(function(deferred) {
$( deferred.resolve );
})
).done(function() {
console.log("TEST");
$( ".test" ).each(function() {
console.log("LOADED 1");
$(this).testscript(); //function from /js/testscript.js
});
});
The second Deferred object becomes resolved as soon as the DOM finishes loading, it does not wait for the getScript() methods (since those could theoretically be executed way later, so they don't get special treatment).
The first Deferred object becomes resolved when
/script.jsfinishes loading, not when all the scripts have finished loading. At that point, the doneCallback to load/scripts2.jsis called, but the doneCallback for the$.when(...)is also already called since both Deferred objects it was passed are resolved at that point.You should put the
$(this).testscript();callback as the doneCallback for thegetScript("/js/testscript.js"), not for thewhen(...)statement, like this: