[英]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:
如果玩家在游戏进行时点击网站上的任何其他地方,我有一个显示 HTML 元素的功能来警告玩家,这里:
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.现在,我正在尝试创建一个类似的函数
adLoseFocus
,旨在通知播放器广告已完成,但在显示之前有 3 秒的延迟。 Phaser itself has a built-in delay function, which I tried here: Phaser 本身有一个内置的延迟功能,我在这里尝试过:
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:不幸的是,这似乎不起作用,所以我已经尝试修改
adLoseFocus
以使用adLoseFocus
代替,这里:
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.由于某种原因,
bIsAd
检查失败,即使它被设置为真正的默认值。 Doing this instead:这样做:
adLoseFocus(true, callbackFunction, true)
ensures that it works.确保它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.