简体   繁体   English

总是在Array中获取最后一项,我需要一个JavaScript闭包吗?

[英]Always get last item in Array, do I need a JavaScript closure?

I have a function that loops through a multidimensional array that consists of the id of an area on my webpage and then a parameter that I have to pass to a webservice. 我有一个循环遍历多维数组的函数,该数组包含我网页上某个区域的id,然后是一个我必须传递给webservice的参数。 The return from the $.ajax() call will be HTML that I wish to populate (repaint) within the first part of the array: $.ajax()调用的返回将是我希望在数组的第一部分中填充(重绘)的HTML:

function getViews(){

    // loop through, need view/jsp name and where we want to put the HTML... need a multidimarray...
    var viewArr = [["infoCol","info"], 
                   ["noteCol", "notes"],
                   ["buttonsDiv", "buttons"],
                   ["historyPanel","history"], 
                   ["servicesPanel","services"],
                   ["noFOUC","dialogs"]
                  ];

    // do the loop
    for(var i = 0;i<viewArr.length;i++){

        var thisArea = viewArr[i][0];

        $.ajax({
            url:"getView",
            type:"POST",
            data:{view:viewArr[i][1]},
            dataType:"html",
            success:function(data){
              console.log(thisArea); // this is always noFOUC
              // console.log(viewArr[i][0]); // this gives an undefined error...
                $("#" + thisArea).html(data);
            },
            error:function(xhr, ajaxOptions, thrownError){
                console.log(xhr.status);
                console.log(xhr.statusText);
                console.log(thrownError);
            }
        }); 

    }
}

Now all is well however trying to reference the first part of the loop viewArr[i][0] within the success: callback isn't working! 现在一切都很好但是试图在成功中引用循环viewArr[i][0]的第一部分:回调不起作用! If I place it within the success: it is undefined. 如果我把它放在成功之中:它是未定义的。 If I give it a var outside the $.ajax() like in the example above it is always the last item of the array! 如果我在$.ajax()之外给它一个var,就像上面的例子一样,它总是数组的最后一项! I'm sure I need to add a closure here but I'm not sure where or why, can someone please explain? 我确定我需要在这里添加一个闭包,但我不知道在哪里或为什么,有人可以解释一下吗?

If I haven't worded or explained this well please let me know and I will explain better. 如果我没有措辞或解释这个,请告诉我,我会更好地解释。

dI think I have this... I placed a self execution function inside the loop... forgot about asynchronous ! 我想我有这个...我在循环中放置了一个自执行函数... 忘了异步 The loop continues... doh! 循环继续... doh!

function getViews(){

    // loop through, need view/jsp name and where we want to put the HTML... need a multidimarray...
    var viewArr = [["infoCol","info"], 
                   ["noteCol", "notes"],
                   ["buttonsDiv", "buttons"],
                   ["historyPanel","history"], 
                   ["servicesPanel","services"],
                   ["noFOUC","dialogs"]
                  ];

    // do the loop
    for(var i = 0;i<viewArr.length;i++){

        (function(){

        var thisArea = viewArr[i][0];
        $.ajax({
            url:"getView",
            type:"POST",
            data:{view:viewArr[i][1]},
            dataType:"html",
            success:function(data){
                $("#" + thisArea).html(data);
            },
            error:function(xhr, ajaxOptions, thrownError){
                console.log(xhr.status);
                console.log(xhr.statusText);
                console.log(thrownError);
            }
        }); 

        })(viewArr[i][0]);

    }
}

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

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