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?

  • Does this answer your question? [What is the purpose of a self executing function in javascript?](https://stackoverflow.com/questions/592396/what-is-the-purpose-of-a-self-executing-function-in-javascript) – Justinas Sep 23 '20 at 13:30

1 Answers1

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
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
  • "Not work for me", yes it works, what exactly have you tried? – Jeremy Thille Sep 23 '20 at 14:43
  • i don t know where i need to add first code with showInterstitialAd(); and showRewardedVideoAd(); – ⴰⴰ ⴰⴰ Sep 23 '20 at 14:46
  • 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. – Thomas Sep 29 '20 at 01:56