简体   繁体   English

一段时间后如何使HTML5函数关闭脚本?

[英]How to make a HTML5 function to close a script after a certain period of time?

I am making a banner with HTML5 and I need it to close after 20 seconds. 我正在用HTML5制作横幅,我需要20秒后关闭它。

It should be something with dhtml.external.close(); 它应该与dhtml.external.close(); and settimeout after 20s. 并在20秒后设置settimeout。 Can anyone help me with this? 谁能帮我这个?


Maybe it is possible to do something in such way? 也许有可能以这种方式做某事?

 var close = document.createElement('a');
                close.style.display = 'block';
                close.href = "#";
                close.innerHTML = 'CLOSE';
                close.onclick = function() {
                    dhtml.external.close();
                }
                document.body.appendChild(close);

            });

but just set the time out somehow? 但是只是以某种方式设定超时时间?

Supposing your banner element has an id "banner" , can do this : 假设您的banner元素具有一个id "banner" ,可以这样做:

<div id=banner>BANNER !</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​

<script>
document.onload=function(){
  ​setTimeout(function(){
     var banner = document.getElementById('banner');
     banner.parentNode.removeChild(banner);    
  }, 20000);​
};
</script>

Demonstration 示范

Note that many libraries help you making prettiest animation at removal. 请注意,许多库可帮助您在删除时制作最漂亮的动画。 For example jQuery allows you to fade it out slowly : 例如,jQuery允许您慢慢淡出它:

setTimeout(function(){
   $('#banner').fadeOut(2000);
}, 20000);​

Demonstration 示范


EDIT : 编辑:

Starting from your comment, I can propose you this : 从您的评论开始,我可以向您提出以下建议:

var close = document.createElement('a');
close.style.display = 'block';
close.href = "#";
close.innerHTML = 'CLOSE';
close.onclick = function() {
     dhtml.external.close(); // is that a call to a library ? Is that IE specific ?
}
document.body.appendChild(close);
setTimeout(function(){
    document.body.removeChild(close);
}, 20000);

Demonstration 示范

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM