简体   繁体   English

如何使用钩子以固定的时间间隔触发功能,并且在满足某些条件时我想清除时间间隔?

[英]How to trigger a function at regular intervals of time using hooks and when certain criteria is met I want to clear the time interval?

I have a react component that performs a certain task at regular intervals of time after mounting.我有一个反应组件,它在安装后定期执行某个任务。 But I want to clear the interval once after a criterion is met.但我想在满足标准后清除一次间隔。 How can I achieve that?我怎样才能做到这一点?

My code我的代码

const [totalTime, setTotalTime] = React.useState(10000);

const foo = () => {
      console.log("Here");
  };

React.useEffect(() => {
    const secondInterval = setInterval(() => {
      if (totalTime > 0) setTotalTime(totalTime - 1000);
    }, 1000);
    return () => clearInterval(secondInterval);
  });

React.useEffect(() => {
    let originalInterval;
    if (totalTime > 0)
      originalInterval = setInterval(() => {
        foo();
        console.log(totalTime);
      }, 5000);
    return () => clearInterval(originalInterval);
  }, []);

When I watch the console even after 10000ms It is still logging Here and also totalTime is always being 10000ms.当我在 10000 毫秒后观看控制台时,它仍在记录此处,并且totalTime始终为 10000 毫秒。 I am not able to figure out what exactly is happening.我无法弄清楚到底发生了什么。

You may need to pass the older state as an argument to the setTotalTime updater function.您可能需要将旧状态作为参数传递给setTotalTime更新程序函数。 You also may need to pass (another) state variable as a dependency to the useEffect hook so that the function is executed every time the state variable changes您可能还需要将(另一个)状态变量作为依赖项传递给useEffect钩子,以便在每次状态变量更改时执行该函数

React.useEffect(() => {
    const secondInterval = setInterval(() => {
      if (totalTime > 0) setTotalTime(totalTime => totalTime - 1000);
    }, 1000);
    return () => clearInterval(secondInterval);
  }, [...]);

Depends on your criteria, and what you call a criteria, but you could just use another state and then useEffect on that another state:取决于你的标准,你所谓的标准,但你可以只使用另一种状态,然后useEffect对另一种状态:

const [criteriaIsMet,setCriteriaIsMet] = useState(false);
useEffect(() => { if(criteriaIsMet) {clearInterval(...)} },[criteriaIsMet])

And then somewhere when you do your actual "criteria logic" you just go ahead and do setCriteriaIsMet(true)然后在某个地方,当您执行实际的“标准逻辑”时,您只需继续执行setCriteriaIsMet(true)

And how would you know Id of interval in above useEffect - again you could just create a special state that will store that id, and you could set it in your original useEffect你怎么知道上面useEffect的间隔 Id - 你可以再创建一个特殊的状态来存储那个 id,你可以在你原来的useEffect设置它

As for why your current code is not clearing the interval is because when you use useEffect with empty array as second argument, and return function in first function argument - that will be execute on component unmounting至于为什么你当前的代码没有清除间隔是因为当你使用useEffect和空数组作为第二个参数,并在第一个函数参数中返回函数 - 这将在组件卸载时执行

And these are just one of numerous options you have actually :)这些只是您实际拥有的众多选择之一:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM