0

I want to toggle between interstitial ads and rewarded video ads in my game html use it construct 2 every time loading layout like if first runtime show interstitial and if loading again show rewarded video ads and repeat this every time .

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

my testcode aftert toggle between two functions automatically

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

i try this but is not work?

1

1 Answer 1

1

It doesn't work because you're not persisting anything on page reload, so you get the exact same page and same code, so the behaviour is exactly the same. You can't toggle this way. Store a state in the localStorage and read it on page load.

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
Sign up to request clarification or add additional context in comments.

3 Comments

"Not work for me", yes it works, what exactly have you tried?
i don t know where i need to add first code with showInterstitialAd(); and showRewardedVideoAd();
You could go as far as to write const currentState = localStorage.getItem("state") === "rewarded" ? "interstitial" : "rewarded"; but for that, OP needs to understand that in this snippet, the else-case also covers the initial state and has to phrase the ternary condition accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.