所以我隐藏了这个图像,5秒钟后它变得可见,但我也希望它在一个循环中进行,所以在5秒后它会被隐藏然后再次可见。
这就是我所拥有的:
//Shows after interval
function show() {
document.getElementById("igpng").style.visibility = "visible";
}
setTimeout("show()", 5000); // after 5 secs
<div id='igpng' style="visibility:hidden; margin:0 auto; height: 100px; max-width: 400px">
<img src="img/soon.png" alt="" style="max-width:100%;" />
</div>
答案 0 :(得分:1)
使用setTimeout
代替运行一次的setInterval
。然后在你的函数中检查图像是否可见,如果是,则将其隐藏(反之亦然)
//Shows after interval
function toggle() {
document.getElementById("igpng").style.visibility = (document.getElementById("igpng").style.visibility == "visible") ? "hidden" : "visible";
}
setInterval(toggle, 5000); // after 5 secs
<div id='igpng' style="visibility:hidden; margin:0 auto; height: 100px; max-width: 400px">
<img src="http://www.placehold.it/100x100" alt="" style="max-width:100%;" />
</div>