Browse Source

看视频间隔功能优化

薛鸿潇 5 years ago
parent
commit
b4b6f41677
1 changed files with 14 additions and 13 deletions
  1. 14 13
      assets/script/mk/system/AdSystem.ts

+ 14 - 13
assets/script/mk/system/AdSystem.ts

@@ -35,6 +35,7 @@ export default class AdSystem {
      * @param callBack 广告相关的回调
      */
     public watchAd(callBack: Function) {
+        if (this.isAdCooling()) return;
 
         this.videoAdcallBack = callBack;
 
@@ -75,8 +76,8 @@ export default class AdSystem {
      */
     public wacthAdSuccess(placementId = '') {
         mk.console.log("wacthAdSuccess");
-        // if (this.isAdCooling()) return;
         this.videoAdcallBack(true);
+        this.initLastTimeStamp();
     }
 
     /** 显示原生广告 */
@@ -111,26 +112,26 @@ export default class AdSystem {
         }
     }
 
-    /** 时评冷却倒计时 */
-    private _ad_cooling_time: number = 0;
+    /** 上次播放视频时间戳,单位:毫秒 */
+    private last_time_stamp: number = 0;
+    private initLastTimeStamp() {
+        let new_date = new Date();
+        this.last_time_stamp = new_date.getTime();
+    }
 
     /**
      * 视频是否冷却
      * @returns 视频冷却中
      */
     private isAdCooling(): boolean {
-        if (this._ad_cooling_time > 0) {
-            mk.tip.pop('当前看视频太频繁了,请' + this._ad_cooling_time + '秒后再试');
+        let new_date = new Date();
+        let new_time_stamp = new_date.getTime();
+        let gap_time_stamp = new_time_stamp - this.last_time_stamp;
+        if (gap_time_stamp <= 5000) {
+            const time = 6 - Math.ceil(gap_time_stamp / 1000);
+            mk.tip.pop('当前看视频太频繁了,请' + time + '秒后再试');
             return true;
         }
-        this._ad_cooling_time = 5;
-        this.runCooling();
         return false;
     }
-    private runCooling() {
-        this._ad_cooling_time--;
-        if (this._ad_cooling_time > 0) {
-            setTimeout(this.runCooling.bind(this), 1000);
-        }
-    }
 }