Javascript 下面是提取一些十六进制数据、解释它并将其打印到console.log的代码。。在';需求';循环,但不在外部。为什么?

Javascript 下面是提取一些十六进制数据、解释它并将其打印到console.log的代码。。在';需求';循环,但不在外部。为什么?,javascript,binary,console,int32,Javascript,Binary,Console,Int32,下面是一些工作代码,用于提取一些十六进制数据,对其进行解释并将其打印到console.log。。它在'req'循环中工作,但在它之后不工作。为什么? <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script src="js-struct.js"></script> <script>

下面是一些工作代码,用于提取一些十六进制数据,对其进行解释并将其打印到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>
所以我的问题是,为什么我不能读取zebuffer,使用Simplestruct函数获取所需的数据,并将其以十进制格式打印到控制台的任何位置,而不是在req.onload和req.send(null)函数之间


我怀疑这是因为onload函数中的代码总是最后发生,所以任何控制台打印都是在数据读入zebuffer之前发生的。这有什么办法吗?e、 g我可以调用一个函数来获取数据并返回脚本,然后它会记录它(或者我想对名为b[]的int32数组执行的任何操作)如果可能,我怎么做

加载请求时执行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()
req.onload=功能(ev){
var arrayBuffer=req.response;//注意:不是oReq.responseText
if(arrayBuffer){
var byteArray=新的Int8Array(arrayBuffer);
对于(变量i=0;i

如果您确实需要它是同步的(相当于异步的),您可以如上所述发送它,其中也描述了上述行为

在加载请求时执行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()
req.onload=功能(ev){
var arrayBuffer=req.response;//注意:不是oReq.responseText
if(arrayBuffer){
var byteArray=新的Int8Array(arrayBuffer);
对于(变量i=0;i

如果你真的需要它是同步的(相当于异步的),你可以将它发送到上面提到的行为也有描述的地方

是的,这很好地解决了问题,感谢Zedd对细节的关注和花在这方面的时间!帮了大忙!你好,丹尼尔。是的,这很好地发挥了作用,谢谢Zedd对细节的关注和花在这上面的时间!帮了大忙!你好,丹尼尔。