Next.js Discord

Discord Forum

Making request every 1 second

Answered
Caillou posted this in #help-forum
Open in Discord
I recently started with "appDir", but when using an effect to make infinite requests with 1 second between them, only the first rendering request is made, not triggering the others after time. Are there any specific changes to make this work?

(Replaced fetch with console.log for better testing)


   React.useEffect(() => {     
     const abort = new AbortController();      
     const id = setTimeout(() => {

       console.log("TEST");        

      }, 1000)
     return () => {
       clearTimeout(id);
       abort.abort();
     };
   }, []);
Answered by Giant panda
You have to use setInterval instead.
View full answer

4 Replies

Giant panda
You have to use setInterval instead.
Answer
Giant panda
Also I'd highly discourage you from manually polling like this and use react-query which covers all edge cases during polling.
@Giant panda You have to use `setInterval` instead.
I feel like a complete idiot 🤡
@Giant panda Also I'd highly discourage you from manually polling like this and use react-query which covers all edge cases during polling.
It's just a proof of concept, so the implementation itself doesn't need that much refining. Thank you for your help!