简体   繁体   English

为什么当我调用函数“StopTimer”函数时我的变量“intervalId”没有更新值?

[英]Why my variable "intervalId" is not getting the value updated when I call the function "StopTimer" function?

Here is my simple code:这是我的简单代码:

import React, { useState } from "react";
import "./App.css";

function App() {
  const [time, setTime] = useState(0);
  var intervalId;

  function startTimer() {
    intervalId = setInterval(() => {
      setTime(time => time + 1);
    }, 1000);
    console.log("My interval id is: ", intervalId);
  }

  function stopTimer() {
    console.log("Clearing intervalId", intervalId);
    clearInterval(intervalId);
  }

  return (
    <div className="App">
      <div>{time}</div>
      <div>
        <button onClick={startTimer}>Start time</button>
        <button onClick={stopTimer}>Stop time</button>
      </div>
    </div>
  );
}

Here is the representation of the error 这是错误的表示

I press Start Time button and then the Stop Time Button , but my variable intervalId in the stopTimer function is not getting the updated value from the setInterval function.我按下Start Time 按钮,然后按下Stop Time Button ,但是我在 stopTimer 函数中的变量 intervalId 没有从 setInterval 函数中获取更新的值。 Why?.为什么?。

Because intervalId is not the same variable that was in scope when startTimer was invoked.因为intervalId与调用startTimer时作用域内的变量不同。 It will be undefined in all the subsequent renders.它将在所有后续渲染中undefined ( caused when time change state ) (当time改变状态时引起)

In React, if you want to "keep a value" across the life cycle of a component, you can use a ref for it:在 React 中,如果你想在组件的整个生命周期中“保持一个值”,你可以为它使用ref

  // ....
  var intervalId = React.useRef();
  // ....
  intervalId.current = setInterval(() => {
  // ...
  clearInterval(intervalId.current);

Since the App is invoked everytime you change the state ( setTime ), you get a new, fresh value of the local variable.由于每次更改状态 ( setTime ) 时都会调用App ,因此您将获得局部变量的新值。

One of options is to include the variable in your state.一种选择是在您的状态中包含变量。

function App() {
  var [time, setTime] = useState(0);
  var [intervalId, setIntervalId] = useState(0);

  function startTimer() {
    setIntervalId(intervalId => setInterval(() => {
      setTime(time => time + 1);
    }, 1000));
  }

  function stopTimer() {
    clearInterval(intervalId);
  }

  return (
    <div className="App">
      <div>{time}</div>
      <div>
        <button onClick={startTimer}>Start time</button>
        <button onClick={stopTimer}>Stop time</button>
      </div>
    </div>
  );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我有一个分配给函数调用值的变量,如果函数调用的参数发生更改,是否可以更新该变量? - If I have a variable, assigned to the value of a function call, can that variable be updated if the function call's parameters are changed? 从函数jquery获取更新的变量值 - getting updated variable value from function jquery 为什么我在将 function 调用分配给变量时得到未定义,即使它是同步调用? - Why I'm getting undefined while assigning a function call to a variable even when it is a synchronous call? 当我调用 function javascript 时,我的 function 变量重置 - my function variable reset when I call function javascript 为什么我的变量没有从 JS function 获得赋值? - Why my variable not getting assigned value from JS function? 当我调用 function 时,我应该将函数中返回的值分配给 function 之外的变量,以获得更新的值吗? - When I call a function, should I assign the values that are returned in the functions to the variables outside the function, to get the updated value? 当我尝试调用我的函数时,为什么会出现“未定义”? - Why am I getting an 'undefined' when I try to call my function? 在尝试导入我的函数时,为什么会出现“TypeError:Assignment to constant variable”? - Why am I getting “TypeError: Assignment to constant variable” when trying to import my function? 为什么当我调用具有返回值的函数时,我的onclick事件不起作用? - Why when i call a function with returning value, my onclick event doesn't work? 变量在函数更新值之前获取输出 - variable getting output before it's value is updated by a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM