您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
setInterval函数怎么在React中使用?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
一、setInterval函数
setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。
setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的参数。
import React, { Component } from 'react';
import { Radio, Button, Icon } from 'antd';
class List extends Component {
constructor(props) {
super(props);
this.state = {
online: false,
};
};
handleLogin=()=>{
localStorage.setItem('username','xuzheng');
};
handleLogout=()=>{
localStorage.removeItem('username');
};
componentDidMount(){
this.timer = setInterval(() => {
this.setState({
online: localStorage.username ? true : false,
})
}, 1000);
}
componentWillUnmount() {
if (this.timer != null) {
clearInterval(this.timer);
}
}
render() {
return (
<div>
<div>
<Icon type='user' style={{marginRight:'8px'}}/>
<span>{localStorage.username ? localStorage.username : '未登录'}</span>
</div>
<div style={{marginTop:'20px'}}>
<Button type='primary' onClick={this.handleLogin}>登录</Button>
</div>
<div style={{marginTop:'20px'}}>
<Button type='primary' onClick={this.handleLogout}>退出</Button>
</div>
</div>
)
}
}
export default List;关于setInterval函数怎么在React中使用问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。