JS定时器暂停与开始

首次发布:2017-08-20 18:44
    <script type="text/javascript">
        var setIntervalID;
        window.onload = function () {
            document.getElementById('btn1').onclick = function () {
                setIntervalID = setInterval(function () {
                    document.getElementById('txt1').value++;
                },1000)
            };
            document.getElementById('btn2').onclick = function () {
                clearInterval(setIntervalID);
            };           
        }
    </script>

    <input id="txt1" type="text" value="0">
    <input id="btn1" type="button" value="开始">
    <input id="btn2" type="button" value="暂停">