Skip to main content

Command Palette

Search for a command to run...

WEB Worker DEMO

Updated
1 min read
  • Requirement: a web server, here we use python to run a simple web server

      python.exe -m SimpleHTTPServer 8080
      #For python3, use this command:
      python3 -m http.server 8080
    
  • The HTML, master code

      <!DOCTYPE HTML>
      <html>
          <head>
              <title>WEB Workder DEMO</title>
              <script>
              //Add version number to avoid cache;
              let workerJsFile = "worker.js?version=" + Date.now();
              var worker = new Worker(workerJsFile);
    
              worker.onmessage = function (event) {
                  document.getElementById("content").innerHTML = event.data;
              };
    
              function startTimer() {
                  worker.postMessage("START");
              }
    
              function stopTimer() {
                  worker.postMessage("STOP");
              }
      </script>
              <style type="text/css">
      table {
          border-collapse:true;
          border-spacing:0;
      }
      thead {
          background: lightgray;
      }
      </style>
          </head>
          <body>
              <table border="1px">
                  <thead>
                      <tr>
                          <th>Current Time</th>
                      </tr
                  </thead>
                  <tbody>
                      <tr>
                          <td id="content">
    
      </td>
                      </tr>
                  </tbody>
                  <tfoot>
                      <tr>
                          <td>
                              <button id="start" onclick="startTimer()">Start</button>
                              <button id="stop" onclick="stopTimer()">Stop</button>
                          </td>
                      </tr>
                  </tfoot>
              </table>
          </body>
      </html>
    
  • The JS, worker code

      var runningTimer;
      //await must in async function;
      async function report() {
          //const d = new Date();
          var strPost = "Fail to get time from server";
          const timeServer = "http://worldtimeapi.org/api/timezone/Asia/Shanghai";
          let response = await fetch(timeServer);
          if (response.ok) {
              let json = await response.json();
              strPost=json.datetime;
          }
          this.postMessage(strPost);
      }
      this.addEventListener("message", function (event) {
          let cmd = event.data;
          switch (cmd) {
          case "START":
              this.postMessage("Started");
              clearInterval(runningTimer);
              runningTimer = setInterval(report, 1000);
              break;
          case "STOP":
              this.postMessage("Stopped");
              clearInterval(runningTimer);
              break;
          }
    
      }, false);
    

More from this blog

解决QWidget用winId获取HWND而导致Qt程序无事件消息的问题

问题原因: 当对一个QWidget调用winId时,默认情况下,Qt会对该窗口进行Native化,从而导致如鼠标等事件被其它原生窗口接管,表现出来的现像就是窗口不响应任何事件 解决思路: 防止Qt窗口Native化 解决办法: 第一步,在创建QApplication对象之前设置: QApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings); //注意:Qt::AA_NativeWindows受环境变量 QT_USE_NATI...

Jul 7, 20251 min read

解决因OpenGL渲染窗口高宽比导致图形变形

在 OpenGL 中,由于窗口的 宽高比(aspect ratio) 与绘制内容的坐标系统不一致,图像会出现拉伸、压缩等变形现象。为了解决这个问题,可根据窗口的大小调整投影矩阵,确保图像在视觉上保持原始比例。 先通过glViewPort调整视口 void resizeGL(int w, int h) { glViewport(0, 0, w, h); } 再根据窗口高宽比,计算出合适的投影矩阵,这样体现出来的样式,就是截掉了宽高比之外的内容,显示的内容不变形,正圆就是正圆,不会被拉伸成...

Jun 18, 20251 min read

cmake管理使用了qt的项目的正确使用方法

可将QT5_DIR(包括了bin/inclue/lib等目录的那个基础目录,不是lib/cmake)加到CMAKE_PREFIX_PATH中,防止find_package无法使用 cmake对qt moc有如下几个函数封装: qt_wrap_ui([输出]MOC后的源文件列表 [输入]MOC前的.ui文件) qt_wrap_cpp([输出]MOC后的源文件列表 [输入]MOC前的源文件,通常是包含了Q_OBJECT的.hpp) qt_add_resources([输出]MOC后的源文件列表 [输...

Apr 7, 20251 min read

源赖朝的部落格威力缩小版

28 posts

源赖朝的部落格威力缩小版