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 在ComponentDidMount中停止并启动设置间隔_Javascript_Reactjs_Timer - Fatal编程技术网

Javascript 在ComponentDidMount中停止并启动设置间隔

Javascript 在ComponentDidMount中停止并启动设置间隔,javascript,reactjs,timer,Javascript,Reactjs,Timer,我目前正在制作一个React应用程序,它每秒从API获取数据以更新自身。 然后,我将显示一个表,其中包含每秒刷新的数据 我目前的问题是,我必须允许用户点击td,从而停止计时器,允许他编辑。 完成后,再次启动计时器 我可以停止它,但我不知道如何重新启动它一旦停止。这是我的密码: class App extends Component { state = { data: [], } componentDidMount() { this.timer = setInterv

我目前正在制作一个React应用程序,它每秒从API获取数据以更新自身。 然后,我将显示一个表,其中包含每秒刷新的数据

我目前的问题是,我必须允许用户点击td,从而停止计时器,允许他编辑。 完成后,再次启动计时器

我可以停止它,但我不知道如何重新启动它一旦停止。这是我的密码:

class App extends Component {
  state = {
    data: [],
  }

  componentDidMount() {
    this.timer = setInterval(() => (axios.get("http://127.0.0.1:8000?count=20")
      .then(data => this.setState({ data: data.data }))
      .catch(error => console.log(error))), 1000)
  }

  stopTimer = () => {
    console.log("stoooop")
    clearInterval(this.timer)
  }

  launchTimer = () => {
    ???
  }

哦,当然,我陷入了在componentDidMount中完成这一切的奇怪逻辑中,甚至没有想到这一点。。谢谢!哦,当然,我陷入了在componentDidMount中完成这一切的奇怪逻辑中,甚至没有想到这一点。。谢谢!
class App extends Component {
  state = {
    data: [],
  }

  componentDidMount() {
    this.timer = this.launchTimer();
  }

  stopTimer = () => {
    console.log("stoooop");
    clearInterval(this.timer);
  };

  launchTimer = () => {
    this.timer = setInterval(() => (axios.get("http://127.0.0.1:8000?count=20")
      .then(data => this.setState({ data: data.data }))
      .catch(error => console.log(error))), 1000);
  };
class App extends Component {
    state = {
       data: [],
}

componentDidMount() {
    this.timer = launchTimer();
}

stopTimer = () => {
   console.log("stoooop")
   clearInterval(this.timer)
}

launchTimer = () => {
    setInterval(() => (axios.get("http://127.0.0.1:8000?count=20")
      .then(data => this.setState({ data: data.data }))
      .catch(error => console.log(error))), 1000)
}