Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 删除组件卸载时的设置间隔_Javascript_Reactjs - Fatal编程技术网

Javascript 删除组件卸载时的设置间隔

Javascript 删除组件卸载时的设置间隔,javascript,reactjs,Javascript,Reactjs,这是我获取通知的代码 static contextType = Context; constructor(props) { super(props); this.state = { roleName: null, notCount : null, notificationData:[], gotoNotify:false, } } logOut = () => { Cookies.remove(

这是我获取通知的代码

 static contextType = Context;
  constructor(props) {
    super(props);
    this.state = {
      roleName: null,
      notCount : null,
      notificationData:[],
      gotoNotify:false,
    }
  }

  logOut = () => {
    Cookies.remove('_token');
    Cookies.remove('_ACid');
    Cookies.remove('_ACrole');
    Cookies.remove('_ACname');
  }
  componentDidMount() {
    this.getNotification();
     setInterval(() => {
    this.getNotification();
  }, 10000);
  }
 
  componentWillUnmount(){
    clearInterval();
    console.log("yes")
  }


我想清除此组件何时卸载或何时注销的间隔。请帮助我执行此操作。

跟踪间隔标识符并使用它来清除它:


建造师(道具){
超级(道具);
this.state={…}
this.notificationInterval=null;//不需要,但可以跟踪它
}
componentDidMount(){
这个.getNotification();
this.notificationInterval=setInterval(this.getNotification,10000);
}
组件将卸载(){
clearInterval(这是notificationInterval);
}
作为一个小提示,您可以直接调用
setInterval(fn)
,而无需使用箭头功能:

//这两个调用几乎相等:
setInterval(()=>this.getNotification(),1000);
setInterval(this.getNotification,10000);

您可以使用全局变量来分配间隔id

之后,您可以使用此id清除间隔

staticcontexttype=Context;
让它有效;
建造师(道具){
超级(道具);
此.state={
roleName:null,
notCount:null,
通知数据:[],
gotoNotify:false,
}
}
注销=()=>{
clearInterval(intervalId);
Cookies.删除(“U令牌”);
饼干。去掉(“酸”);
Cookies.移除(“ACrole”);
Cookies。删除(“acu名称”);
}
componentDidMount(){
这个.getNotification();
clearInterval(intervalId);
intervalId=setInterval(()=>{
这个.getNotification();
}, 10000);
}
组件将卸载(){
clearInterval(intervalId);
console.log(“是”)
}

将间隔标识符分配给state,并在调用
clearInterval
时将其传递给state?只需存储间隔id即可
this.intervalId=setInterval(
),然后可以清除它->
clearInterval(this.invervalId)
@DBS我不会使用state,否则您将获得无意义的重新渲染。使用全局变量不是一个好主意,想想组件被多次调用的情况。所有变量都将指向同一个全局实例。最好使用
this.property
,使其成为类/对象的一部分。是这样的您不需要创建几个间隔来通知用户。这就是我使用全局变量的原因。啊,我明白您的意思。在这种情况下,如果给定函数不使用
,则“无箭头函数”版本是合理的。但是,如果它使用,则可能会导致问题,例如