[英]First click doesn't log all info but second click does
Button wont log everything on first click but does on second click. 按钮不会在第一次单击时记录所有内容,但在第二次单击时会记录所有内容。 Pretty much it skips the function that iterate through the object.
它几乎跳过了遍历对象的功能。
Well I've tried putting promises and async await on most function thinking this was the problem, but to no avail. 好吧,我尝试过对大多数功能进行承诺和异步等待,认为这是问题所在,但无济于事。
// button code //按钮代码
const btn = document.querySelector("button");
btn.disabled = false;
btn.onclick = function(e) {
takeASnap()
.then(toDataURL)
.then(async function() {
Object.keys(await returnData).forEach(function(item) {
console.log(item); // key
console.log(typeof item);
console.log(item);
console.log(returnData[item]); // value
});
console.log(await returnData);
});
};
});
<div class="window">
<video></video>
<button class="snapshot">take a snapshot</button>
</div>
async function toDataURL(blob) {
let reader = new FileReader();
let b64;
reader.readAsDataURL(blob);
reader.onloadend = function() {
let base64data = reader.result;
let count = 0;
let data;
// ChunkSubstr takes thousand character and put into array that is
// returned
b64 = chunkSubstr(base64data, 1000);
console.log("Hllo");
webSocket(b64);
};
}
This is the function that assign the returndata it's value which comes from a server. 这是为返回数据分配来自服务器的值的函数。
async function webSocket(b64) {
const ws = new WebSocket("ws://192.168.1.70:3000");
ws.onopen = await function() {
console.log("Connected");
b64.forEach(element => {
ws.send(m_imageNr + " " + element);
// console.log(element);
});
m_imageNr++;
ws.onmessage = function(event) {
console.log(typeof returnData);
returnData.push(event.data);
};
};
return await returnData;
}
Expected result is that it should iterate through the object on first click but it does only do that on the second click. 预期结果是,它应该在第一次单击时遍历对象,但仅在第二次单击时才这样做。
EDITTED added some code that was asked for. 编辑添加了一些要求的代码。
refactoring may help: 重构可能有助于:
const btn = document.querySelector("button");
btn.disabled = false;
async function getData() {
Object.keys(await returnData).forEach(function(item) {
console.log(item); // key
console.log(typeof item);
console.log(item);
console.log(returnData[item]); // value
});
console.log(await returnData);
};
btn.onclick = function(e) {
takeASnap()
.then(toDataURL)
.then(() => await getData());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.