TanStackT
TanStack7mo ago
9 replies
verbal-lime

Refetch at every X seconds

Hey everyone!

I have a question about dynamic refetchInterval behavior. I'm working with a query that should refetch every 60 seconds, but I need it to sync with an external schedule.

Use case: Users can land on the page at any time, but data updates happen on a fixed 60-second server schedule (e.g., at :00, :01, :02 of each minute). If a user arrives at :40, I want the first refetch to happen in 20 seconds (to sync with the :00 mark), then continue every 60 seconds after that.

What's the best way to handle this dynamic interval where the first refetch has a different delay than subsequent ones?

I've considered:
- Using refetchInterval as a function, but it seems to apply the same logic to all intervals
- Manual refetch() calls with useInterval, but wondering if there's a cleaner TanStack Query way
- Setting up an initial timeout, then switching to refetchInterval

Any suggestions for the most tanstack way to handle this?
Was this page helpful?