我想在延迟一段时间后调用onMessageReceived。
window.addEventListener("message", onMessageReceived);
function onMessageReceived(event)
{
var messageData = JSON.parse(event.data);
if (messageData.type === "panelExpanded")
{
expandButton.style.display = 'none';
}
}
当接收到的数据为“ panelExpanded”时,我想隐藏expandButton
我要延迟onMessageRecieved方法。我尝试了setInterval和setTimeout,但没有这项工作。
setTimeout(window.addEventListener("message", onMessageReceived), 3000);
window.addEventListener ('message', function () {
setTimeout (onMessageReceived, 3000);
}, false);
setInterval(window.addEventListener("message", onMessageReceived), 3000);
window.addEventListener ('message', function () {
setInterval (onMessageReceived, 3000);
}, false);
如何添加延迟?帮助将不胜感激。提前致谢
最佳答案
你有这样尝试过吗?
window.addEventListener("message", onMessageReceived);
function onMessageReceived(event)
{
setTimeout(function(){
var messageData = JSON.parse(event.data);
if (messageData.type === "panelExpanded")
{
expandButton.style.display = 'none';
}
},3000);
}