Here is some working code to extract some hex data, interpret it and print it to console.log.. it works while in the 'req' loop, but not after it. why?
<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 is just an abstract sample binary data file with two sets of four int32's to read. the code reads the second set of four int32s.
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
So my question is why can I not read zebuffer, get the required data using Simplestruct function and print it in decimal format to console anywhere but in between the req.onload and req.send(null) functions?
I suspect its because code inside the onload function always happens last so that any console prints happen first before the data gets read into zebuffer. 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 (or whatever else i want to do with the int32 array called b[]) how could i do this if possible?
The onload function is executed when the request has been loaded, and that data is naturally only availible after the request has been sent. The "send" is asynchronous, so code directly after the "send" executes immediately after, and does not wait until the send is completed. You need to continue your program flow when the data has been retrieved, in other words in the onload event handler.
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
Is there any particular reason you want to circumvent this behaviour? You can contiue your program in the onload, or you can just pass the data on to some other function on your 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()
If you really need it to be synchronous (as apposed to asynchronous), you can send it as described at the end of this article where the behaviour mentioned above also is described