2

In React Native, when setTimeout is used to schedule execution of callback and then app is backgrounded, callback is never executed.

Why is that?

I did find several secondary sources which mention this behavior but no comprehensive docs.

clearpath
  • 916
  • 8
  • 24

1 Answers1

-1

Use the AppState event listener provided by react-native :-

  useEffect(() => {
    const subscription = AppState.addEventListener("change", nextAppState => 
  {
      if (
        appState.current.match(/inactive|background/) 
      ) {
         setTimeout(()=>{
          // your callback function
         },1000)
      }

    
    });

    return () => {
      subscription.remove();
    };
  }, []);

Here's the link to docs - https://reactnative.dev/docs/appstate

this.arjun
  • 330
  • 1
  • 4
  • 1
    Thanks for the link, but text there does not mention that there are changes in behavior of timers when app is in bacgtround. – clearpath Mar 08 '22 at 07:46