Answer the question
In order to leave comments, you need to log in
Problem with closures in javascript?
I write dynamic loading of javascripts. There is a list of filenames to be loaded and a list of callbacks to be executed after the download. Type:
onload_js = [];<br>
onload_js[onload_js.length] = {"src":"script1.js","callback":function() { callback1(); }};<br>
onload_js[onload_js.length] = {"src":["script2.js","script3.js"],"callback":function() { callback23(); }};<br>
for (var i in onload_js) {<br>
for (var j in onload_js[i]["src"]) {<br>
$.getScript(onload_js[i]["src"][j],function() {<br>
if (//Здесь проверка что все остальные скрипты загружены тоже)<br>
onload_js[i]["callback"]();<br>
}<br>
}<br>
}<br>
$.getCode(onload_js[i]["src"][j],function(i,j) {<br>
Answer the question
In order to leave comments, you need to log in
var callback = function(x,y){
return function(){
// x = i
// y = j
if (//Здесь проверка что все остальные скрипты загружены тоже)
onload_js[i]["callback"]();
}
}
$.getScript(onload_js[i]["src"][j], callback(i,j) )
The $.getScript doc says:
This is a shorthand Ajax function, which is equivalent to:
$.ajax({
url: url,
dataType: 'script',
success: success
});
This object will be made the context of all Ajax-related callbacks
for (var i in onload_js) {
for (var j in onload_js[i]["src"]) {
$.ajax(
url: onload_js[i]["src"][j],
dataType: 'script',
context: onload_js[i],
success: function() {
if (//Здесь проверка что все остальные скрипты загружены тоже)
this["callback"]();
}
);
}
}
function fireAllInRealOrder(){
for (var j in onload_js[i]["src"]) {
onload_js[i]["callback"]();
}
}
for (var i in onload_js) {
for (var j in onload_js[i]["src"]) {
$.getScript(onload_js[i]["src"][j],function() {
if (//Здесь проверка что все остальные скрипты загружены тоже)
fireAllInRealOrder();
}
}
}
what's stopping you from doing it differently?
If I understand correctly and you need to pass the current value of i, then
(function(i) {
onload_js[i]["callback"]();
})(i);
more precisely
(function(i) {
$.getScript(onload_js[i]["src"][j],function() {
if (//Здесь проверка что все остальные скрипты загружены тоже)
onload_js[i]["callback"]();
}
})(i);
By the way, the for (var i in onload_js) construct is dangerous: i, in addition to the values [0, 1], will take parasitic names of methods and properties of the Array object: $family, each, clean, associate, link, contains, extend, getLast, getRandom , include, combine, erase, empty, flatten, hexToRgb, rgbToHex, toJSON.
To avoid this, an additional wrapper check is needed:
for (var i in onload_js) {
if (onload_js.hasOwnProperty(i)) { /* do something */}
}
var onload_js = [];
onload_js.push({
"src": ["script1.js"],
"callback": function() { callback1(); }
});
onload_js.push({
"src": ["script2.js", "script3.js"],
"callback": function() { callback23(); }
});
function myCoolLoader() {
for (var i = 0; i < onload_js.length; i++) {
var scripts = onload_js[i];
for (var j = 0; j < scripts.src.length; j++) {
var url = scripts.src[j];
$.getScript(url, function() {
if (/*Здесь проверка что все остальные скрипты загружены тоже*/) {
scripts.callback();
}
});
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question