js實現div顯示2秒後自動消失
阿新 • • 發佈:2019-02-02
前言
在做四象限的過程中會有提示的資訊,但是想讓提示資訊在一定的時間後自動隱藏。
如上圖,2s後提示自動隱藏,怎麼做呢?
正文
利用setTimeout(),setTimeout()方法用於在指定的毫秒數後呼叫函式或計算表示式。
語法
setTimeout(code,millisec)
提示和註釋
setTimeout() 只執行 code 一次。如果要多次呼叫,請使用 setInterval() 或者讓 code 自身再次呼叫 setTimeout()。
例項
<html> <head> <script type="text/javascript"> function timedMsg() { var t=setTimeout("alert('5 seconds!')",5000) } </script> </head> <body> <form> <input type="button" value="Display timed alertbox!" onClick="timedMsg()"> </form> <p>Click on the button above. An alert box will be displayed after 5 seconds.</p> </body> </html>