JavaScript,Phaser:使用 setTimeout 调用函数 - 延迟不起作用
JavaScript, Phaser: Calling a function with setTimeout - delay doesn't work
我制作了一个移相器游戏,可以放在网站的框架中。如果玩家在游戏进行时点击网站上的其他任何地方,我有一个功能可以显示 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')));
}
}
以上工作正常。
现在,我正在尝试做一个类似的功能adLoseFocus
,旨在通知播放器广告结束,但在显示前延迟 3 秒。 Phaser本身有一个内置的延迟功能,我在这里尝试过:
this.game.time.events.add(3000, adLoseFocus);
不幸的是,这似乎不起作用,所以我尝试修改 adLoseFocus
以改用 setTimeOut,此处:
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;
}
那也不行;立即显示警告。我错过了什么吗?
我的错误,显然错误在函数调用中。这样做:
adLoseFocus(true, callbackFunction)
由于某种原因未能通过 bIsAd
检查,即使它被设置为 true 默认值。改为这样做:
adLoseFocus(true, callbackFunction, true)
确保它有效。
我制作了一个移相器游戏,可以放在网站的框架中。如果玩家在游戏进行时点击网站上的其他任何地方,我有一个功能可以显示 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')));
}
}
以上工作正常。
现在,我正在尝试做一个类似的功能adLoseFocus
,旨在通知播放器广告结束,但在显示前延迟 3 秒。 Phaser本身有一个内置的延迟功能,我在这里尝试过:
this.game.time.events.add(3000, adLoseFocus);
不幸的是,这似乎不起作用,所以我尝试修改 adLoseFocus
以改用 setTimeOut,此处:
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;
}
那也不行;立即显示警告。我错过了什么吗?
我的错误,显然错误在函数调用中。这样做:
adLoseFocus(true, callbackFunction)
由于某种原因未能通过 bIsAd
检查,即使它被设置为 true 默认值。改为这样做:
adLoseFocus(true, callbackFunction, true)
确保它有效。