简体   繁体   English

Javascript onclick显示带有for循环的数组的最后一个元素

[英]Javascript onclick shows last element of array with for loop

I have little problem. 我没什么问题。 I am working with one companys API's. 我正在使用一个公司的API。 I request for activities and they return me array of activities. 我请求活动,他们返回我的一系列活动。 Here is part of code. 这是代码的一部分。

client.requestActivities(function(activities) {
        if (activities.length > 0) {
            for(i=0; i < activities.length; i++) {
                var activity = activities[i];
                activity.onStart(function() { alert(i+ " started"); });
                activity.onCredit(function() { alert(i+ " credit"); });
                activity.onClose(function() { alert(i+ " close"); });
                activity.onFinish(function() { alert(i+ " finish"); });
                            $('.Game-Screen-Footer').append('<div class="Game-Screen-Footer-Adv" id="foota'+i+'"><a href="javascript:;" ><img src="'+activity.image_url+'" alt="'+activity.display_text+'" width="190" height="110"></a></div>');
                document.getElementById('foota'+i+'').onclick = function() {
                ARNO.box.show({html:'<div id="socialVibeFancyBox0"></div>', close: false, width:activity.window_width, height:activity.window_height, openjs: 
                            function(){
                                client.loadActivityIntoContainer(activity, 'socialVibeFancyBox0');
                            }
                        });
            }
        }

} }

Mostly, this function give back array with 3 elements. 大多数情况下,此函数返回包含3个元素的数组。 But when I click on first activity, onStart it should alert me (0 start), but it alerts (4 start), this means, that it alerts last element of i. 但是当我点击第一个活动时,onStart它应该提醒我(0开始),但是它会发出警报(4开始),这意味着它会警告我的最后一个元素。 How should I fix this? 我该怎么解决这个问题? I tried a lot of stuff, but I haven't find solution, maybe you can help me? 我尝试了很多东西,但我找不到解决方案,也许你可以帮助我?

This is yet another case of closure problems. 这是封闭问题的另一个例子。 Try this: 试试这个:

for( i=0; i<activities.length; i++) {
    (function(i) {
        // your code here
    })(i);
}

Move the guts of the for loop out to a function, and pass in the values that need to be scoped separately. for循环的内容移动到函数中,并传递需要单独作用域的值。

for(i=0; i < activities.length; i++) {
    setUpActivity(i, activities[i]);
}

function setUpActivity(i, activity) {

    activity.onStart(function() { alert(i+ " started"); });
    activity.onCredit(function() { alert(i+ " credit"); });
    activity.onClose(function() { alert(i+ " close"); });
    activity.onFinish(function() { alert(i+ " finish"); });
    ARNO.box.show({
        html:'<div id="socialVibeFancyBox0"></div>', 
        close: false, width:activity.window_width, 
        height:activity.window_height, 
        openjs:function(){
                    client.loadActivityIntoContainer(activity, 'socialVibeFancyBox0');
                }
            });
}

A function invocation creates a new variable scope. 函数调用会创建一个新的变量范围。 any variables or parameters created in the function will be in their own scope, and the functions created in the same scope will have permanent reference to those variables. 在函数中创建的任何变量或参数都在它们自己的范围内,并且在同一范围内创建的函数将永久引用这些变量。

If you don't care about IE8- then ES5 already solves this issue with forEach : 如果你不关心IE8-那么ES5已经用forEach解决了这个问题:

activities.forEach(function(v, i){
  // now 'i' will work anywhere within this scope
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM