如何在没有按钮的情况下自动在两个功能之间切换

时间:2020-09-23 13:29:15

标签: javascript

我想在我的游戏html中的非页内广告和奖励视频广告之间进行切换,每次加载布局时都使用它构造2,例如是否在第一次运行时显示非页内广告,是否再次加载时显示奖励视频广告并每次重复。

SysActs.prototype.GoToLayout = function(to) {
  showInterstitialAd();
  showRewardedVideoAd();
  if (this.runtime.isloading)
    return; // cannot change layout while loading on loader layout
  if (this.runtime.changelayout)
    return; // already changing to a different layout
  ;
  this.runtime.changelayout = to;
};

我的测试代码自动在两个功能之间切换

    SysActs.prototype.GoToLayout = function (to)
    {
  if($(this).data('toggleAds') == 1) {
    toggleAds = 0;
    
        if (this.runtime.isloading || showRewardedVideoAd()) 
            return; 
        if (this.runtime.changelayout )
            return;
;
        this.runtime.changelayout = to;
  }
  else {
    toggleAds = 1;
    
        if (this.runtime.isloading || showInterstitialAd() )
            return;
        if (this.runtime.changelayout )
            return;
;
        this.runtime.changelayout = to;
        showInterstitialAd();
  }

  $(this).data('toggleAds', toggleAds);
  return false;
};

我尝试这个但不行吗?

1 个答案:

答案 0 :(得分:1)

这是行不通的,因为您不会在页面重新加载时保留任何内容,因此您获得的页面和代码完全相同,因此行为也完全相同。您无法以这种方式切换。将状态存储在localStorage中,并在页面加载时读取它。

const previousState = localStorage.getItem("state"); // null, "interstitial" or "rewarded"
let currentState;

if(previousState){
   currentState = previousState === "interstitial" ? "rewarded" : "interstitial";
} else { // First page load ever
   currentState = "rewarded"; // or "interstitial", initialize it like you want
}

localStorage.setItem("previousState", currentState); // It's saved for next reload

// Now do something with currentState