难以使用全局变量和$ .getScript [英] Having difficulty working with global variables and $.getScript

查看:94
本文介绍了难以使用全局变量和$ .getScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本无法正常工作:

I have the following script which does not work properly:

function getWidgetContent( widget ) {
    if(widget.script!=null){
        $global_widget_id = widget.widget_id;
        $.getScript( "js/" + widget.script, function() {
            $( ".widget_header_title_" + widget.widget_id ).append( widget.title );
        });
    }
}

调用如下:

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

我认为这会运行函数,在函数内,赋值给全局值,然后在循环的每次迭代中运行 $。getScript 。但那并没有发生。它似乎循环遍历循环,直到循环结束,让我们说它循环3次,每次为全局值赋值,即3次,然后它最终去并且 $。 getScript

I thought this would run the function, within the function, assign a value to the global value, then run the $.getScript per iteration of the loop. But that is not happening. It seems to iterate through the loop until the loop is finished, lets say it loops 3 times, assign a value to the global value each time, i.e. 3 times, and then it finally goes and does the $.getScript.

无法工作的Ofcourse现在只使用$ .getScript文件中的全局值的最后一次赋值3次...

Ofcourse that will not working as it will now only use the last assignment of the global value in the $.getScript file 3 times...

如何更改此值以便为全局变量赋值,运行 $。getScript 。完成后,继续使用原始循环,将下一个值分配给全局变量,运行 $。getScript ,直到循环结束。

How do I change this so it assigns a value to the global variable, runs the $.getScript. When done, continue with the original loop, assign the next value to the global variable, run the $.getScript until the loop has finished.

推荐答案

你得到这个是因为$ .getScript是一个异步方法。在这种情况下,这意味着该方法在脚本完成加载之前立即返回,并继续在其后执行代码。

You're getting this because $.getScript is an asynchronous method. In this case, it means that the method returns immediately before the script has finished loading, and continues executing code after it.

这意味着类似:

$.getScript('a.js', function () {
    console.log('Loaded a');
    }); 
$.getScript('b.js', function () {
    console.log('Loaded b');
    }); 
$.getScript('c.js', function () {
    console.log('Loaded c');
    }); 
// Output could be:
// Loaded c
// Loaded a
// Loaded b

这意味着所有脚本文件请求可以同时完成,但这也意味着订单不是确定性的(固定)。

This means that all of the script files requests can be done simultaneously but it also means that the order is not deterministic (fixed).

您可以确保通过链接 getWidgetContent 如果您使用的是jQuery 1.5及更高版本,请http://wiki.commonjs.org/wiki/Promises/A =nofollow>承诺
但是,这种方法的缺点是你同时加载所有脚本请求,请求将在前一个完成后一个接一个地发送。

You can ensure that the getWidgetContent is called sequentially by chaining promises if you are using jQuery 1.5 and above. However, the pitfall of this method is that you will not be concurrently loading all the script requests at the same time, the requests will be sent one after another after the previous one has completed.

确保返回$ .getScript的结果,该结果是延迟对象(我在这里做了很少的更改,只需注意 return 语句):

Make sure you return the result of $.getScript which is a Deferred Object (I made minimal changes here, just note the return statements):

function getWidgetContent( widget ) {
  if(widget.script!=null){
    $global_widget_id = widget.widget_id;
    return $.getScript( "js/" + widget.script, function() {
        $( ".widget_header_title_" + widget.widget_id ).append( widget.title );
        });
  }
  return null;
}

在履行上一个承诺后执行新getWidgetContent的新方法(完成上一步行动):

New method to perform a new getWidgetContent after a previous promise is fulfilled (completion of previous action):

function doGetWidgetContentAfter(promise, widget) {
  return promise.then(function () {
      return getWidgetContent( widget );
      });
}

调用它:

var promise = $.when(true);
for ( j = 0; j <= widget_data.d.length - 1; j++ ) {
  promise = doGetWidgetContentAfter( promise, widget_data.d[j] );
}



说明



doGetWidgetContentAfter 做的是当承诺完成时然后调用 getWidgetContent 小部件的功能然后方法返回一个在任何内部方法保证完成时完成的保证。

Explanation

What doGetWidgetContentAfter does is that when the said promise is complete then call the getWidgetContent function for widget. The then method returns a promise that completes when any internal methods promise completes.

我知道它开始听起来很复杂,但要玩,并试验它。 :)

I know it's starting to sound complicated, but do play around and experiment with it. :)

这篇关于难以使用全局变量和$ .getScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆