Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.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/8/.htaccess/6.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 使用更少资源的setInterval的替代方案_Javascript_Reactjs_Setinterval - Fatal编程技术网

Javascript 使用更少资源的setInterval的替代方案

Javascript 使用更少资源的setInterval的替代方案,javascript,reactjs,setinterval,Javascript,Reactjs,Setinterval,我有一个组件,每4秒更改一次图像,如下所示: state = { images: this.props.items, imageIndex: 0, } componentDidMount() { this.interval = setInterval(() => this.changeImage(), 4000) } changeImage = () => { const { imageIndex, images } = this.state if (imageI

我有一个组件,每4秒更改一次图像,如下所示:

state = {
  images: this.props.items,
  imageIndex: 0,
}
componentDidMount() {
  this.interval = setInterval(() => this.changeImage(), 4000)
}
changeImage = () => {
  const { imageIndex, images } = this.state
  if (imageIndex === images.length - 1) {
    this.setState({ imageIndex: 0 })
  } else {
    this.setState({ imageIndex: imageIndex + 1 })
  }
}
render() {
  const { images, imageIndex } = this.state
  return <img src={images[imageIndex]} />
}
状态={
图片:this.props.items,
图像索引:0,
}
componentDidMount(){
this.interval=setInterval(()=>this.changeImage(),4000)
}
changeImage=()=>{
const{imageIndex,images}=this.state
if(imageIndex==images.length-1){
this.setState({imageIndex:0})
}否则{
this.setState({imageIndex:imageIndex+1})
}
}
render(){
const{images,imageIndex}=this.state
返回
}
该组件在页面上的6个点中使用

问题是,几分钟后,风扇打开,电脑开始发热。我不知道这是否是由于CPU使用量增加或内存泄漏造成的

在使用较少计算机资源的情况下,是否有任何替代方法可以
setInterval
(以预定义的间隔执行重复的任务?

尝试以下方法:

尝试:


为此目的使用
setInterval
本身并没有什么错误。但是您应该确保在卸载组件时清除间隔!为此,有一个名为
clearInterval
的函数。这很可能是性能问题的根源

所以我建议如下:

componentWillUnmount() {
  clearInterval(this.interval);
}

为此目的使用
setInterval
本身并没有什么错误。但是您应该确保在卸载组件时清除间隔!为此,有一个名为
clearInterval
的函数。这很可能是性能问题的根源

所以我建议如下:

componentWillUnmount() {
  clearInterval(this.interval);
}

是否希望每个组件都有自己的间隔?一种方法可能只有一个间隔,并且与所有组件共享相同的间隔。组件是否已卸载?对于6个不同的组件,这总是相同的任务吗?通过将此.interval设置为静态并检查它是否存在,您只能创建一个interval。@Fabioantune是的,不幸的是它们是独立的interval。@ChrisR我无法卸载该组件,因为那样会删除图像。但是关于你的第二个问题——是的,所有组件的任务都是一样的,但是它们有不同的时间间隔,所以我不认为我可以把它移走。你有没有在chrome开发工具中描述过这一点
setInterval
基本上不消耗资源,它调用的回调可能会消耗资源。大概如果没有更多关于
changeImage
实际功能的详细信息,我们也无法为您提供更多帮助。您希望每个组件都有自己的间隔吗?一种方法可能只有一个间隔,并且与所有组件共享相同的间隔。组件是否已卸载?对于6个不同的组件,这总是相同的任务吗?通过将此.interval设置为静态并检查它是否存在,您只能创建一个interval。@Fabioantune是的,不幸的是它们是独立的interval。@ChrisR我无法卸载该组件,因为那样会删除图像。但是关于你的第二个问题——是的,所有组件的任务都是一样的,但是它们有不同的时间间隔,所以我不认为我可以把它移走。你有没有在chrome开发工具中描述过这一点
setInterval
基本上不消耗资源,它调用的回调可能会消耗资源。大概如果没有更多关于
changeImage
实际功能的详细信息,我们也无法为您提供更多帮助。
每4秒一次?真的吗?@Manoj你有一个像样的反驳吗?@castis,
setTimeout
在这种情况下不合适,因为它不会定期工作。
每4秒一次
?真的吗?@Manoj你有合适的反驳吗?@castis,
setTimeout
在这种情况下不合适,因为它不会定期工作。