Browse Source

bugfix:优化来回切换频繁调用接口问题

leo 1 year ago
parent
commit
916e2a5c30
1 changed files with 32 additions and 7 deletions
  1. 32 7
      static/webRtcYJ/WXtrialInterface.html

+ 32 - 7
static/webRtcYJ/WXtrialInterface.html

@@ -410,6 +410,24 @@
       });
     </script>
     <script>
+      // 如果immediate为true,就是立即执行防抖,反之就是延迟防抖
+      function debounce(fn, delay, immediate) {
+        let timeout;
+        return function () {
+          let _self = this;
+          let args = arguments;
+          if (immediate && !timeout) {
+            fn.apply(_self, args);
+            leadingExecuted = true;
+          }
+          clearTimeout(timeout);
+          timeout = setTimeout(() => {
+            timeout = null;
+            if (!immediate) fn.apply(_self, args);
+          }, delay);
+        };
+      }
+
       let isVideo = !!(
         typeof RTCPeerConnection !== 'undefined' &&
         typeof RTCIceCandidate !== 'undefined' &&
@@ -417,12 +435,10 @@
       );
       let engine = null;
       let connectData = null;
-      window.addEventListener('message', function (e) {
-        let data = e.data;
-        if (data.bool && isVideo) {
+      let debounceSwitchingScreens = debounce(
+        function () {
           $('#playVideo').hide();
           $('#playCanvas').show();
-          document.getElementById('playVideo').play();
           $('.weui-mask_transparent').show();
           $('.weui-toast').show();
           engine.disconnect();
@@ -430,7 +446,16 @@
           decodeWoker.postMessage('close');
           decodeWoker.terminate();
           webSocketWorker.postMessage('close');
-          connect()
+          connect();
+        },
+        2000,
+        true,
+      );
+      window.addEventListener('message', function (e) {
+        let data = e.data;
+        if (data.bool && isVideo) {
+          document.getElementById('playVideo').play();
+          debounceSwitchingScreens();
         }
       });
       // 开发环境
@@ -1574,7 +1599,7 @@
       // 获取云机剩余时长
       function getResidueTime() {
         clearInterval(residueTimer);
-        if (![1,2,3].includes(userCardType)) {
+        if (![1, 2, 3].includes(userCardType)) {
           return;
         }
         return $.ajax({
@@ -1608,7 +1633,7 @@
 
       // 客户端成功进入云手机后调用
       function startTime() {
-        if (![1,2,3].includes(userCardType)) {
+        if (![1, 2, 3].includes(userCardType)) {
           return;
         }
         return $.ajax({