定時器(setInterval)和延時器(setTimeout)

注意: 

 

//組件掛載完畢
        componentDidMount(){
            console.log("componentDidMount()掛載完畢---")
            this.intervalId = setInterval(function () {
                console.log("定時器被開啓---")
                let{opacity} = this.state
                opacity -=0.1
                if(opacity<=0){
                    opacity=1
                }
                this.setState({opacity})
            }.bind(this),200)
        }

        // componentWillUpdate () {
        //     console.log('componentWillUpdate(): 將要更新---')
        // }
        // componentDidUpdate () {
        //     console.log('componentDidUpdate(): 已經更新---')
        // }

        //組件將要卸載的時候,清除定時器
        componentWillUnmount(){
            clearInterval(this.intervalId)
        }

 

發佈了55 篇原創文章 · 獲贊 12 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章