문제 :자바 스크립트 생성기 기능
나는 다음과 같은 스크립트가 어디에 기본적으로, 나는 세계의 데이터에서 ID를 저장하는 기능에 데이터를 전달 아약스 호출에서 데이터를 얻고있다 변수, 전역 변수가 jQuery의 $.getScript()
에서 검색 다른 스크립트에서 사용할 수 있도록 :
스크립트 (script1.js) :이 비트는 단지 아약스를 통해 데이터의 비트 (도시하지 않음)에 도착
,하지만에 있어요. 556,678,916,322,139,789,803,210하고,이 경우, widget_data.d
의 데이터의 길이에 따라 getWidgetContent()
기능을 실행해야 3 개 반복 :
function getWidgetContent(widget) {
if(widget.script!=null){
window.global_widget_id = widget.widget_id;
$.getScript("js/script2.js", function() {
alert("direct title variable in script1.js: " + widget.title);
alert("global variable in script1.js: " + window.global_widget_id);
alert("direct variable in script1.js: " + widget.widget_id);
$(".widget_header_title_" + widget.widget_id).append(widget.title);
});
}
}
스크립트 :이 기능이 실행 위의 루프
window.global_widget_id = "";
for (j = 0; j <= widget_data.d.length - 1; j++) {
getWidgetContent(widget_data.d[j]);
}
(script2.js) :
이 함수는 전역 변수도 전달하므로 전역 적으로 저장된 ID를 기반으로 아약스를 통해 데이터를 가져와야합니다.
var my_widget_id = window.global_widget_id;
alert("global variable in script2.js " + window.global_widget_id);
alert("direct variable in script2.js: " + my_widget_id);
// then do some more ajax stuff with global_widget_id before repeating the loop again.
실제 결과 :
global variable in script2.js: 66
direct variable in script2.js: 66
direct title variable in script1.js: title for 57 goes here
global variable in script1.js 66
direct variable in script1.js 57
global variable in script2.js: 66
direct variable in script2.js: 66
direct title variable in script1.js: title for 65 goes here
global variable in script1.js 66
direct variable in script1.js 65
global variable in script2.js: 66
direct variable in script2.js: 66
direct title variable in script1.js: title for 66 goes here
global variable in script1.js 66
direct variable in script1.js: 66
예상 결과 : 나는 시도 무엇
global variable in script2.js: 57
direct variable in script2.js: 57
direct title variable in script1.js: title for 57 goes here
global variable in script1.js 57
direct variable in script1.js 57
global variable in script2.js: 65
direct variable in script2.js: 65
direct title variable in script1.js: title for 65 goes here
global variable in script1.js 65
direct variable in script1.js 65
global variable in script2.js: 66
direct variable in script2.js: 66
direct title variable in script1.js: title for 66 goes here
global variable in script1.js 66
direct variable in script1.js: 66
:
this website을 기반으로하면 은 일 수 있습니다. generator function
을 만듭니다. 다음은 템플릿입니다 :
(function(variable) {
return function() {
// do something with variable
}
})(value);
내가 이것을 사용하여 시도했지만, 아무것도, 오류, 알림 없음, 아무것도, 즉 발생하지 :
for (j = 0; j <= widget_data.d.length - 1; j++) {
var the_data = widget_data.d[j];
(function(the_data) {
return function() {
getWidgetContent(the_data );
}
})(the_data);
}
질문 :
을왜 발전기 기능이 작동하지 않습니까?
라고 내가 언급하지 않은 것은 내가 사용자의 선택에 따라 필요한 경우에만 $ .getScript를 사용하여 호출 할 것이다 작은 스크립트의 수백 것입니다 도착 어디에서 볼 수 없습니다. 처음부터 모든 스크립트를 호출하면 다운로드 속도가 느려지고 다운로드 시간이 늘어납니다. – oshirowanen