2012-12-10 8 views
1

문제 :자바 스크립트 생성기 기능

나는 다음과 같은 스크립트가 어디에 기본적으로, 나는 세계의 데이터에서 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); 

} 

질문 :

왜 발전기 기능이 작동하지 않습니까?

답변

0

당신이하고있는 일은 getWidgetContent을 호출하는 함수를 반환하고 있습니다.

(function(the_data) { 
    return function() { // you will return a function, it will not get called. 
    getWidgetContent(the_data ); 
    } 
})(the_data); // the (the_data) means you call the first function with the_data as parameter. 

당신이 getWidgetContent를 호출 할 경우

, 당신은 직접 호출해야합니다.

(function(the_data) { 
    return getWidgetContent(the_data ); 
})(the_data); 
// this will call your anonymous function with the_data as parameter, giving closure. 

내가 볼 수있는 것에서 만 이것은 유일한 관심사는 아닙니다. script1.js 출력을 인쇄하는 success 함수는 script2.js가 실행 된 후, 아마도로드 루프 후에 호출됩니다.

편집 : 시간 script2.js으로 당신은 당신이 그것을 덮어 쓰기 때문에, 65, 66,하지만, script2.js는 마지막 값, 66을보고 57 글로벌 변수 3 시간을 설정 한로드됩니다. 루프는 스크립트 다운로드보다 훨씬 빠르며 비동기입니다.

당신이해야 할 일은 script2 코드를 함수에 넣고 위젯을 매개 변수로 사용하여 script1 success 콜백에서 호출하는 것입니다. 단지

function getWidgetContent(widget) { 

    if(widget.script!=null){ 

    window.global_widget_id = widget.widget_id; 

    $.getScript("js/script2.js", function(inner_widget) { 
     return function() { 
      // this is the function that will get called as the callback. 
      alert("direct title variable in script1.js: " + inner_widget.title); 
      alert("global variable in script1.js: " + window.global_widget_id); 
      alert("direct variable in script1.js: " + inner_widget.widget_id); 
      // inner_widget is the parameter, so it is in a closure 
      $(".widget_header_title_" + inner_widget.widget_id).append(inner_widget.title); 
      //make your call to script2.js if you want. 
      script2.run(inner_widget); 
     } 
    }(widget)); 
    //we call the generator function with the widget to get closure. 
    } 
} 
+0

라고 내가 언급하지 않은 것은 내가 사용자의 선택에 따라 필요한 경우에만 $ .getScript를 사용하여 호출 할 것이다 작은 스크립트의 수백 것입니다 도착 어디에서 볼 수 없습니다. 처음부터 모든 스크립트를 호출하면 다운로드 속도가 느려지고 다운로드 시간이 늘어납니다. – oshirowanen

0

밤은 인도 표준시 :

for (j = 0; j <= widget_data.d.length - 1; j++) { 

    var the_data = widget_data.d[j]; 

    (function(the_data) { 
    getWidgetContent(the_data); 
    })(the_data); 

} 

? 그렇지

반환 된 기능이