简体   繁体   中英

JavaScript, Phaser: Calling a function with setTimeout - delay doesn't work

I made a phaser game that's to be put in a frame on a website. Should the player click anywhere else on the website while the game is going, I have a function that shows HTML elements to warn the player, here:

function showOnLoseFocus()
{    
    if( window.globalGame !== null && window.globalGame !== undefined )
    {
        document.getElementById('container_refocusWarning').style.display = 'block';        
        document.getElementById('refocusTitle').innerHTML = ((getHtmlTextFromXML('restore_default') === '' ? 'Game Paused' : getHtmlTextFromXML('restore_default')));
        document.getElementById('refocusResume').innerHTML = ((getHtmlTextFromXML('restore_tap') === '' ? '(Tap to resume game)' : getHtmlTextFromXML('restore_tap')));
    }
}

The above works just fine.

Now, I'm trying to make a similar function adLoseFocus , designed to notify the player of an ad finishing, but with a delay of 3 seconds before it shows. Phaser itself has a built-in delay function, which I tried here:

this.game.time.events.add(3000, adLoseFocus);

Unfortunately, this doesn't seem to work, so I've since tried modifying adLoseFocus to use setTimeOut instead, here:

function adLoseFocus( bIsMuted, p_callback, bIsAd = true)
{
    adRegainMute = bIsMuted;
    adActive = true;
    window.globalGame.sound.mute = true;
    if( window.globalGame !== null && window.globalGame !== undefined && bIsAd)
    {
        setTimeout(function() {            
            document.getElementById('container_refocusWarning').style.display = 'block';
            document.getElementById('refocusTitle').innerHTML = getHtmlTextFromXML('restore_ad');
            document.getElementById('refocusResume').innerHTML = getHtmlTextFromXML('restore_tap');
        }, 3000);
    }
    fAdCallback = p_callback;
}

That doesn't work either; the warning is shown immediately. Am I missing something?

My mistake, apparently the error was in the function call. Doing this:

adLoseFocus(true, callbackFunction)

for some reason fails the bIsAd check, even though it's set to true default. Doing this instead:

adLoseFocus(true, callbackFunction, true)

ensures that it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM