Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.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
Reactjs 如何更改React中的间隔值_Reactjs_Setinterval - Fatal编程技术网

Reactjs 如何更改React中的间隔值

Reactjs 如何更改React中的间隔值,reactjs,setinterval,Reactjs,Setinterval,我尝试在使用ReactJs时更改间隔值,但似乎无法使其正常工作。我已经查过了,但没有发现任何有用的东西,或者可能是我在阅读时没有找到它。我的大部分代码都在类组件中 我的代码如下所示: class Timer extends React.Component { constructor(props) { super(props); this.state = { tickS: 1000, ... }; } componentDidMount

我尝试在使用ReactJs时更改间隔值,但似乎无法使其正常工作。我已经查过了,但没有发现任何有用的东西,或者可能是我在阅读时没有找到它。我的大部分代码都在类组件中

我的代码如下所示:

class Timer extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      tickS: 1000,

      ...
    };
  }
  componentDidMount() {
    this.intervalID = setInterval(
      () => this.tick(),
      this.state.tickS
    );
  }
  tick(){
    this.setState({
      ...

      tickS: 10**Math.log10(this.state.tickS + this.state.clicksChange),

      ...
    });
  }
}

它在使用1000毫秒时确实有效,但当我尝试重新定义滴答声的状态时,它不起作用。

您不能更改setInterval的间隔。相反,您可以采用另一种方法并使用setTimeout实现相同的目的。请查看以下示例:

class Timer extends React.Component {
  constructor(props) {
    super(props);
    this.ticks = 1000;
  }

  componentDidMount() {
    this.timeoutID = setTimeout(
      () => this.tick(),
      this.tick
    );
  }

  tick() {
    this.setState({
      ...
    });

    this.ticks = 10 ** Math.log10(this.ticks + this.state.clicksChange),

    this.timeoutID = setTimeout(
      () => this.tick(),
      this.ticks
    ); 
  }
}

所以你想更新间隔触发的频率?不是它触发的频率,而是我希望它不断变化,因此每个间隔都比之前的间隔慢:亲爱的,很乐意帮忙: