Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using getScript synchronously

Tags:

I'm writing an engine that requires the use of getScript quite extensively. I've pushed it into its own function, for ease of use, but now I need to make sure that the function itself is synchronous. Unfortunately, I can't seem to make getScript wait until the script it loads is actually finished loading before proceeding. I've even tried setting jQuery's ajax asynch property to false before making the call. I'm thinking of using jQuery's when/done protocol, but I can't seem to wrap my head around the logic of placing it inside a function and making the function itself synchronous. Any help would be very much appreciated!

function loadScript(script){ //Unrelated stuff here!!! $.when( $.getScript(script,function(){     //Unrelated stuff here!!! })).done(function(){     //Wait until done, then finish function }); } 

Loop code (by request):

for (var i in divlist){         switch($("#"+divlist[i]).css({"background-color"})){             case #FFF:             loadScript(scriptlist[0],divlist[i]);             break;         case #000:             loadScript(scriptlist[2],divlist[i]);             break;         case #333:             loadScript(scriptlist[3],divlist[i]);             break;         case #777:             loadScript(scriptlist[4],divlist[i]);             break;     } } 
like image 488
VashGH Avatar asked Feb 08 '13 23:02

VashGH


1 Answers

This worked for me, and may help you.

$.ajax({     async: false,     url: "jui/js/jquery-ui-1.8.20.min.js",     dataType: "script" }); 

Basically, I just bypassed the shorthand notation and added in the async: false

like image 145
Sachseb1 Avatar answered Oct 07 '22 18:10

Sachseb1