js 定时器的案例,开始,结束,数字滚动

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <style>
        #ula li {
            color: royalblue;
        }
    </style>


    <!-- <div id="box1">
        <div id="box2">
            <div id="box3">
                <div id="box4"></div>
            </div>
        </div>
    </div> -->

    <!-- <input type="text" id="abc" />
    <div id="te"></div>
    <div id="ta"></div> -->

    <div id="gd">0</div>
    <input type="submit" id="ks" value="开始" /><input type="submit" id="js" value="结束" />

    <script>
        //定时器
        // var a = 0;
        // setInterval(function() {
        //     console.log(a++);
        // }, 1000);

        //定时器案例开始,结束,数字滚动。
        var gd = document.getElementById('gd');
        var ks = document.getElementById('ks');
        var js = document.getElementById('js');
        var a = 0;
        var cset;

        ks.onclick = function() {
            clearInterval(cset);
            cset = setInterval(function() {
                gd.innerHTML = a++;
            }, 1000);
        }

        js.onclick = function() {
            clearInterval(cset);
        }
    </script>
</body>

</html>

发表评论

邮箱地址不会被公开。