1

这是一些工作代码,用于提取一些十六进制数据,对其进行解释并将其打印到 console.log .. 它在“req”循环中工作,但不在它之后。为什么?

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script src="js-struct.js"></script>

    <script>
    // Question: does the onload stuff happen last that could be why the struct is zero.

var zebuffer = new ArrayBuffer(128);
var inputter = new Int8Array(zebuffer);

var url = "data.bin";

var req = new XMLHttpRequest();
req.open("GET", url, true);
req.responseType = "arraybuffer";

if( req.overrideMimeType ) {
    req.overrideMimeType('text/plain; charset=x-user-defined');
} 
else {
    req.setRequestHeader('Accept-Charset', 'x-user-defined');
}


req.onload = function (ev) {
var arrayBuffer = req.response; // Note: not oReq.responseText
  if (arrayBuffer) {
    var byteArray = new Int8Array(arrayBuffer);
        for (var i = 0; i < byteArray.byteLength; i++) {
    // do something with each byte in the array

                inputter[i] = byteArray[i];

            }


var SimpleStruct = Struct.create(
            Struct.int32("x"),
            Struct.int32("y"),
            Struct.int32("z"),
            Struct.int32("blank")
 ); 

var b = SimpleStruct.readStructs(zebuffer, 0, 2);

//console.log("zebuffer = "+zebuffer);
//console.log("inputter = "+inputter[0]);

console.log("b[1].x = "+b[1].x);        
console.log("b[1].y = "+b[1].y);
console.log("b[1].z = "+b[1].z);
console.log("b[1].blank = "+b[1].blank);
console.log("------------------------------------------------");    

   }

};

req.send(null);



    </script>
</head>
<body>
    <p>Seamus! open the browser console</p>
</body>

data.bin 只是一个抽象的示例二进制数据文件,有两组要读取的四个 int32。代码读取第二组四个 int32。

FF FF FF 3F FF FF FF 3F FF FF FF 3F FF FF FF 7F 
1A 1A 1A 1A FF FF FF 3F 00 00 00 7F FF FF FF 1F

所以我的问题是为什么我不能读取 zebuffer,使用 Simplestruct 函数获取所需的数据并以十进制格式打印到控制台,但在 req.onload 和 req.send(null) 函数之间?

我怀疑这是因为 onload 函数中的代码总是最后发生,因此任何控制台打印都会在数据被读入 zebuffer 之前首先发生。有没有办法解决?例如,我能否以某种方式调用一个函数,该函数获取数据并返回脚本,然后控制台记录它(或我想对名为 b[] 的 int32 数组执行的任何其他操作)如果可能,我该怎么做?

4

1 回答 1

0

onload 函数在请求加载后执行,数据自然只有在请求发送后才可用。“发送”是异步的,所以“发送”之后的代码直接执行,不会等到发送完成。检索到数据后,您需要继续您的程序流程,换句话说,在 onload 事件处理程序中。

 Is there any way around this? e.g Can i somehow call a function which gets the data and comes back to the script and then it console.logs it

你有什么特别的理由要规避这种行为吗?您可以在 onload 中继续您的程序,或者您可以将数据传递给 onload 上的其他函数

req.onload = function (ev) {
    var arrayBuffer = req.response; // Note: not oReq.responseText
    if (arrayBuffer) {
        var byteArray = new Int8Array(arrayBuffer);
        for (var i = 0; i < byteArray.byteLength; i++) {
            inputter[i] = byteArray[i];
        }
        var SimpleStruct = Struct.create(Struct.int32("x"), Struct.int32("y"),  Struct.int32("z"), Struct.int32("blank")); 
        var b = SimpleStruct.readStructs(zebuffer, 0, 2);
        myNewFunction(b) //pass your data on to some other function
    }
}

function myNewFunction(data) {
    //do things with 'data'
}

req.send()

如果您确实需要它是同步的(与异步相反),您可以按照本文末尾的描述发送它,其中也描述了上述行为

于 2015-09-02T09:45:37.543 回答