react实现定时器

class App extends Component {
state = {
       seconds:new Date().toLocaleTimeString()
        }

    ticking=()=> {
        const {seconds}=this.state
        this.setState({
            seconds:new Date().toLocaleTimeString()
        })
    }

    componentDidMount(){
        this.interval = setInterval(()=>this.ticking(),1000)
        }
    componentWillUnmount() {
        clearInterval(this.interval);
        }
render(){
  <div className="App">
   <label id="labeltime" style={{marginLeft: (window.innerWidth - window.innerWidth / 7), 
     fontSize: 15}}>当前时间:{this.state.seconds}
   </label>
   </div>
   }
}

 

展开阅读全文

没有更多推荐了,返回首页