clearInterval() 无法停止在计时器上运行的间隔

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

clearInterval() fails to stop an interval running on a timer

问题

首次使用clearInterval(),查看其他示例和间隔文档,似乎这是停止间隔的方法。不确定我漏掉了什么。

意图是在currentStop属性更新时停止计时器。

  1. import React, { useEffect, useState } from 'react';
  2. type Props = {
  3. stopNumber: number;
  4. currentStop: number;
  5. };
  6. const timerComponent = ({ stopNumber, currentStop }: Props) => {
  7. let interval: NodeJS.Timer;
  8. // 在计时器上更新已过时间
  9. useEffect(() => {
  10. if (stopNumber === currentStop) {
  11. interval = setInterval(() => {
  12. console.log('timer is running');
  13. }, 3000);
  14. // 在卸载时清除间隔
  15. return () => clearInterval(interval);
  16. }
  17. }, []);
  18. // 清除正在运行的计时器
  19. useEffect(() => {
  20. if (stopNumber !== currentStop) {
  21. clearInterval(interval);
  22. }
  23. }, [currentStop]);
  24. };
英文:

First time using clearInterval() looking at other examples and the interval docs this appears to be the way to stop an interval. Not sure what I am missing.

The intention is to kill the timer when the currentStop prop updates.

  1. import React, { useEffect, useState } from 'react';
  2. type Props = {
  3. stopNumber: number;
  4. currentStop: number;
  5. };
  6. const timerComponent = ({ stopNumber, currentStop }: Props) => {
  7. let interval: NodeJS.Timer;
  8. // Update elapsed on a timer
  9. useEffect(() => {
  10. if (stopNumber === currentStop) {
  11. interval = setInterval(() => {
  12. console.log('timer is running');
  13. }, 3000);
  14. // Clear interval on unmount
  15. return () => clearInterval(interval);
  16. }
  17. }, []);
  18. // Clear timers that were running
  19. useEffect(() => {
  20. if (stopNumber !== currentStop) {
  21. clearInterval(interval);
  22. }
  23. }, [currentStop]);
  24. };

答案1

得分: 1

使用 ref 来存储间隔 ID。

  1. let interval = useRef();
  2. // 启动 setInterval:
  3. interval.current = setInterval(...);
  4. // 停止 setInterval:
  5. clearInterval(interval.current);
英文:

Use a ref to store the interval id instead.

  1. let interval = useRef();
  2. // to start the setInterval:
  3. interval.current = setInterval(...);
  4. // to stop the setInterval:
  5. clearInterval(interval.current);

答案2

得分: 1

将intervalId存储在一个ref中

  1. const timerComponent = ({ stopNumber, currentStop }: Props) => {
  2. const intervalRef = useRef({
  3. intervalId: 0
  4. })
  5. // 在定时器上更新已经过去的时间
  6. useEffect(() => {
  7. if (stopNumber === currentStop) {
  8. intervalRef.current.intervalId = setInterval(() => {
  9. console.log('计时器正在运行');
  10. }, 3000);
  11. // 在卸载时清除定时器
  12. return () => clearInterval(intervalRef.current.intervalId);
  13. }
  14. }, []);
  15. // 清除正在运行的定时器
  16. useEffect(() => {
  17. if (stopNumber !== currentStop) {
  18. clearInterval(intervalRef.current.intervalId);
  19. }
  20. }, [currentStop]);
  21. };
英文:

Store the intervalId on a ref instead

  1. const timerComponent = ({ stopNumber, currentStop }: Props) => {
  2. const intervalRef = useRef({
  3. intervalId: 0
  4. })
  5. // Update elapsed on a timer
  6. useEffect(() => {
  7. if (stopNumber === currentStop) {
  8. intervalRef.current.intervalId = setInterval(() => {
  9. console.log('timer is running');
  10. }, 3000);
  11. // Clear interval on unmount
  12. return () => clearInterval(intervalRef.current.intervalId);
  13. }
  14. }, []);
  15. // Clear timers that were running
  16. useEffect(() => {
  17. if (stopNumber !== currentStop) {
  18. clearInterval(intervalRef.current.intervalId);
  19. }
  20. }, [currentStop]);
  21. };

huangapple
  • 本文由 发表于 2023年2月9日 01:15:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389395.html
匿名

发表评论

匿名网友

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

确定