javascript数组的范围从IE9到FireFox/Chrome有所不同 [英] scope of javascript array differs from IE9 to FireFox/Chrome

查看:99
本文介绍了javascript数组的范围从IE9到FireFox/Chrome有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经解决了我的基本问题:数组的范围从IE9到FireFox(和Chrome)都不一样.

I've narrowed down my fundamental problem: the scope of an array differs from IE9 to FireFox (and Chrome).

在以下函数(仅摘录)中,我声明一个数组,然后使用对$ .getJSON()的调用来填充它.因为引用HoldEm的行发生在ProcessArray函数内(甚至在boolSortArray分支内),所以我推测sortedWorking那时将可用.它在IE9中,但不在FireFox/Chrome中.在引用的行中,FireFox/Chrome中的sortedWorking为空.在任何浏览器中都不会发出错误.

In the following function (excerpt only), I declare an array, then fill it with a call to $.getJSON(). Because the line referencing HoldEm occurs within the ProcessArray function (and even within the boolSortArray branch), I have presumed that sortedWorking would be available at that point. It is in IE9 but not in FireFox/Chrome. In the line quoted, sortedWorking is empty in FireFox/Chrome. No errors are issued in any browser.

实验表明,sortedWorking填充在标记为"$ .getJSON结束"的行之前,而在该行之后为空.有什么想法吗?

Experiments show that sortedWorking is populated just before the line noted as "end of $.getJSON," while just after that line it is empty. Any thoughts?

function ProcessArray(arWorking, boolSortArray, idX, isPartners, isChildren) {
//...
var sortedWorking = [];
if(boolSortArray) {
  $.getJSON('MurakiMaida.json', function(data) {
    $.each(data.person, function(i, xdata) {
    ...
    sortedWorking.push(targetID + ":" + birthYear);
    ...
    }); //end of $.each
  });   //end of $.getJSON

  var HoldEm = BubbleSort(sortedWorking);

推荐答案

您对"$ .getJSON()"的调用是异步.您不能依赖该调用后的代码行中要填充的数组.而是将依赖于要填充的数组的代码放在其完成处理程序的内部 .

Your call to "$.getJSON()" is asynchronous. You can't rely on the array to be populated in the lines of code following the call. Instead, put your code that relies on the array being populated inside the completion handler for it.

function ProcessArray(arWorking, boolSortArray, idX, isPartners, isChildren) {
//...
var sortedWorking = [];
if(boolSortArray) {
  $.getJSON('MurakiMaida.json', function(data) {
    $.each(data.person, function(i, xdata) {
    ...
    sortedWorking.push(targetID + ":" + birthYear);
    ...
    }); //end of $.each

     var HoldEm = BubbleSort(sortedWorking);
     // ... whatever else ...
  });   //end of $.getJSON

现在,这也可能意味着您需要重新考虑"ProcessArray"函数本身,因为类似地,在返回 still 后,您无法确定该数组是否已被填充.通常,完成方法是遵循与"$ .getJSON()"本身完全相同的模式:向"ProcessArray()"添加一个回调参数,以便其客户端可以传递在数组具有已被提取和排序,以及其他所有操作.

Now this may also mean that your "ProcessArray" function itself will need to be re-thought, because, similarly, after it returns you still cannot be sure that the array will have been populated. Generally the way that is done is to follow exactly the same pattern that "$.getJSON()" itself follows: add a callback parameter to "ProcessArray()" so that its clients can pass in a function to be invoked when the array has been fetched and sorted and whatever else is done.

这篇关于javascript数组的范围从IE9到FireFox/Chrome有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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