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

How can I cancel Interval polling? #236

Closed
zhangking opened this issue Jan 19, 2020 · 5 comments
Closed

How can I cancel Interval polling? #236

zhangking opened this issue Jan 19, 2020 · 5 comments

Comments

@zhangking
Copy link

I use refreshInterval to implement the Interval polling. But I'd know how to cancel it when it's running

@shuding
Copy link
Member

shuding commented Jan 19, 2020

You can use a state for the interval, and set it to 0 when you want to cancel:

const [int, setInt] = useState(3000)
const { data } = useSWR('/api', fetcher, { refreshInterval: int })

return <button onClick={() => setInt(0)}>cancel</button>

@zhangking
Copy link
Author

thks

@Gavinchen92
Copy link

You can use a state for the interval, and set it to 0 when you want to cancel:

const [int, setInt] = useState(3000)
const { data } = useSWR('/api', fetcher, { refreshInterval: int })

return <button onClick={() => setInt(0)}>cancel</button>

dont work

@shuding
Copy link
Member

shuding commented Jan 20, 2020

@Gavinchen92 Can you give me more details? Here's a working example: https://codesandbox.io/s/exciting-zhukovsky-jxm18

@SevenOutman
Copy link

SevenOutman commented Sep 2, 2020

Same problem here. In React devtools you see refreshInterval: 0 in useSWR hook, but the polling doesn't stop. Opened a new issue #632 for detailed explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants