我正在编写一个需要广泛使用getScript的引擎.为了便于使用,我已将其推入自己的功能,但现在我需要确保函数本身是同步的.不幸的是,我似乎无法让getScript等到它加载的脚本在继续之前实际完成加载.我甚至尝试在调用之前将jQuery的ajax asynch属性设置为false.我正在考虑使用jQuery的when/done协议,但我似乎无法理解将其置于函数内并使函数本身同步的逻辑.任何帮助将非常感谢!
function loadScript(script){
//Unrelated stuff here!!!
$.when(
$.getScript(script,function(){
//Unrelated stuff here!!!
})).done(function(){
//Wait until done, then finish function
});
}
Run Code Online (Sandbox Code Playgroud)
循环代码(按要求):
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;
}
}
Run Code Online (Sandbox Code Playgroud)