用JS使广告窗口倒计时5秒后关闭

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body,
        html {
            height: 100%;
        }

        div {
            display: fixed;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            margin: auto;
            height: 300px;
            width: 500px;
            line-height: 300px;
            text-align: center;
            background-color: #ccc;
            margin-top: 100px;
        }

        div span {
            color: red;
        }
    </style>
</head>

<body>
    <div id="ads">
        <h1>这是一则广告,在<span id="count5s">5</span>秒后自动关闭!</h1>
    </div>

    <script>
        function closeAds() {
            document.getElementById('ads').style.display = "none";
        }
        var oCount5s = document.getElementById("count5s").innerText
        var second = parseInt(oCount5s)
        function countDown() {
            second -= 1;
            document.getElementById("count5s").innerText = second;

        }
        var countDown = setInterval(countDown, 1000)
        setInterval(function () {

            if (second === 0) {
                closeAds();
                clearInterval(countDown);
            }
        }, 1000)



    </script>
</body>

</html>

 

posted @ 2019-08-20 16:04  wangbing111  阅读(3545)  评论(0编辑  收藏  举报