我想在延迟一段时间后调用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);
我如何添加延迟?帮助将不胜感激。提前致谢
答案 0 :(得分:1)
你试过这样的吗?
window.addEventListener("message", onMessageReceived);
function onMessageReceived(event)
{
setTimeout(function(){
var messageData = JSON.parse(event.data);
if (messageData.type === "panelExpanded")
{
expandButton.style.display = 'none';
}
},3000);
}