javascript生成器功能不起作用

时间:2012-12-10 13:47:28

标签: javascript jquery

问题:

我有以下脚本,基本上,我从ajax调用获取数据,将数据传递给函数,将数据中的id存储在全局变量中,因此全局变量可以在检索的不同脚本中使用来自jQuery' $.getScript()

脚本(script1.js):

这个位只是通过ajax获取了一些数据(未显示),但它位于widget_data.d中,它应该根据getWidgetContent()中的数据长度运行widget_data.d函数{1}},在本例中为3次迭代:

window.global_widget_id = "";

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

    getWidgetContent( widget_data.d[j] );

}

这是上面循环运行的函数:

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 );

        });


    }

}

脚本(script2.js):

这是上述函数也传递全局变量的脚本,然后应该根据全局存储的id通过ajax获取数据。

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);

}

问题:

为什么发电机功能不起作用?

2 个答案:

答案 0 :(得分: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输出的成功函数在运行script2.js之后调用,并且可能在加载循环之后调用。

修改 加载script2.js时,你已经用57,65,66设置了全局变量3次,但是由于你覆盖它,script2.js只能看到最后一个值66.你的循环比脚本下载快得多,这是异步的。

我认为你应该做的是将你的script2代码放在一个函数中,并以widget作为参数从script1成功回调中调用它。

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.
  }
}

答案 1 :(得分: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);

}

否则我无法看到返回函数的调用位置