clearInterval 不起作用,计数器继续

huangapple go评论93阅读模式
英文:

clearInterval don't work, counter continue

问题

我在一个REACTjs函数组件中使用了这部分代码,我的计数器开始得很好,但clearInterval没有效果,它仍然在计数... 我通过"console.log"检查了这段代码的每一行是否在预定的时间调用。存储由redux toolkit管理。
"startCounter"只是旋转一个小动画,表示计数器正在运行。谢谢你的关注。

  1. // 开始 = 启动器 -> true
  2. // 停止 = 启动器 -> False
  3. function setTimer (starter) {
  4. let interv = setInterval(setClock, 1000);
  5. setStartCounter(true);
  6. if (!starter) {
  7. clearInterval(interv);
  8. setStartCounter(false);
  9. }
  10. }
  11. function setClock () {
  12. dispatch(toggleTimeCountTask(task.id));
  13. }

"interv"变量仅在此函数中声明并使用,我查看了一些类似的主题,但找不到解决方案。

英文:

I use this portion of code in a REACTjs function component, my counter starts fine, but the clearInterval has no effect, it still counts... I checked with "console.log" that each line of this code is called at the desired time. The store is managed by redux toolkit.
"startCounter" just spins a mini animation indicating that the counter is running. Thank you for your attention.

  1. // Start = starter -> true
  2. // Stop = starter -> False
  3. function setTimer (starter) {
  4. let interv = setInterval(setClock, 1000);
  5. setStartCounter(true);`
  6. if (!starter) {`
  7. clearInterval(interv);
  8. setStartCounter(false); `
  9. }
  10. }
  11. function setClock () {`
  12. dispatch(toggleTimeCountTask(task.id));`
  13. }

The "interv" variable is declared and used only in this function, I went through some similar topics, but couldn't find the solution.

答案1

得分: 1

你在函数内部实例化了 setInterval,因此会导致两个定时器同时运行。你应该将它移到函数外部,如下所示:

  1. const interv = useRef(null);
  2. function setTimer (starter) {
  3. if(!interv.current) {
  4. interv.current = setInterval(setClock, 1000);
  5. }
  6. setStartCounter(true);
  7. if (!starter) {
  8. clearInterval(interv.current);
  9. setStartCounter(false);
  10. interv.current = null;
  11. }
  12. }
  13. function setClock () {
  14. dispatch(toggleTimeCountTask(task.id));
  15. }

如果这是在一个函数式组件内部,你需要在组件销毁时进行清理:

  1. useEffect(() => {
  2. return () => {
  3. if(interv.current) {
  4. clearInterval(interv.current);
  5. }
  6. };
  7. }, []);
英文:

You are instantiating the setInterval inside your function. So two executions will result in two timers. You should move it outside the function as so:

  1. const interv = useRef(null);
  2. function setTimer (starter) {
  3. if(!interv.current) {
  4. interv.current = setInterval(setClock, 1000);
  5. }
  6. setStartCounter(true);
  7. if (!starter) {
  8. clearInterval(interv.current);
  9. setStartCounter(false);
  10. interv.current = null;
  11. }
  12. }
  13. function setClock () {
  14. dispatch(toggleTimeCountTask(task.id));
  15. }

If this is inside a functional component, you need to cleanup if the component is destroyed:

  1. useEffect(() => {
  2. return () => {
  3. if(interv.current) {
  4. clearInterval(interv.current);
  5. }
  6. };
  7. }, []);

huangapple
  • 本文由 发表于 2023年2月24日 05:43:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定