简体   繁体   English

第一次点击不会记录所有信息,但第二次点击会记录

[英]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);
            });

        };
      });

HTML 的HTML

 <div class="window">
 <video></video>
  <button class="snapshot">take a snapshot</button>
 </div>

toDataUrl toDataUrl

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);
      };
    }

webSocket function webSocket功能

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.

相关问题 第一次单击元素不起作用,第二次单击起作用 - First click of element doesn't work, second click does jQuery不会在第一次点击时触发,但会在第二次点击时触发 - jQuery doesn't fire on first click but does on second innerHTML 在第一次单击按钮时有效,但在第二次单击时无效 - innerHTML works on first button click but doesn't work on second click 它在第一次点击时不起作用,但在第二次点击时起作用。 如何解决? - It doesn't work on the first click, but does on the second one. How to fix that? 单击功能不适用于第二次单击 - click function doesn't work for second click React onClick 在第一次点击时没有触发,第二次点击行为符合预期(简单的调试消息直到第二次点击才会出现) - React onClick not firing on first click, second click behaves as expected (simple debug message doesn't appear until second click) <a href=“..”>第一次点击不起作用,但在FF中第二次点击起作用,在IE中工作正常</a> - <a href=“..”> doesn't work at first click but works at second click in FF, in IE works fine 第一次单击无法初始化我的变量,但第二次单击可以 - Initializing my variable doesn't works by first click, but by second click it works ng-click不会在第一次单击时激活滚动指令,而是在第二次单击时激活滚动指令? - ng-click doesn't activate scroll-to directive on first click but second? JQGrid数据不会在第二次或以后的单击事件中重新加载…(它在第一次单击getdata按钮时加载) - JQGrid data doesn't reload on second or subsequent click events…(it loads on first getdata button click)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM