js---计时器

64 阅读1分钟

计时器:


             
                通过定时器,可以使代码在指定时间后执行
                    - 设置定时器的方式有两种:
                        setTimeout()
                            - 参数:
                                1. 回调函数(要执行的代码)
                                2. 间隔的时间(毫秒)
                            - 关闭定时器
                                clearTimeout()

                        setInterval() (每间隔一段时间代码就会执行一次)
                            - 参数:
                                1. 回调函数(要执行的代码)
                                2. 间隔的时间(毫秒)
                            - 关闭定时器
                                clearInterval()

            

            const timer = setTimeout(()=>{
                alert("我是定时器中的代码")
            }, 3000)

            // clearTimeout(timer)


            const numH1 = document.getElementById("num")

            // let num = 0

            // const timer = setInterval(() => {
            //     num++
            //     numH1.textContent = num

            //     if(num === 200){
            //         clearInterval(timer)   num加到200时计时器就被清除了
            //     }
            // }, 30)


textContent:修改文字内容