Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useInterval clear后,如何重启? #1829

Open
nuonuonuonuoyan opened this issue Aug 24, 2022 · 4 comments
Open

useInterval clear后,如何重启? #1829

nuonuonuonuoyan opened this issue Aug 24, 2022 · 4 comments
Labels

Comments

@nuonuonuonuoyan
Copy link

useInterval clear后,如何重启?

@kongmoumou
Copy link
Contributor

根据源码,更新 delay 即可

useEffect(() => {
    if (!isNumber(delay) || delay < 0) return;

    if (immediate) {
      fnRef.current();
    }
    timerRef.current = setInterval(() => {
      fnRef.current();
    }, delay);
    return () => {
      if (timerRef.current) {
        clearInterval(timerRef.current as NodeJS.Timer);
      }
    };
  }, [delay]);

@crazylxr
Copy link
Collaborator

@nuonuonuonuoyan nu 解决你的问题了不?

@wangxm87
Copy link

有问题,如果clear后重启时delay不变,那么定时器重启不了

@crazylxr
Copy link
Collaborator

合理,不过加这个需要改动 api 了,下个大版本加

@crazylxr crazylxr reopened this Apr 23, 2023
@crazylxr crazylxr added the v4 label Apr 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants