I am trying to make a timeout page when loading certain data from the backend takes too long, but i am failing at something very easy (i think), my code currently
const Layout = ({ lang: { language, loadingLang }, getLang }) => {
const time = useRef(0);
const timer = useRef(false);
useEffect(() => {
// Get the language
if (!language && !loadingLang) {
getLang();
//Start the timer
timer.current = setInterval(function() {
++time.current;
console.log(time.current);
}, 1000);
return () => {
clearInterval(timer.current);
};
}
// Prevent any useless errors with net line:
// eslint-disable-next-line
}, []);
if (language && !loadingLang) {
// stop the timer if whe have the data
clearInterval(timer.current);
}
if (!language && time.current > 10) {
return <LoadingTimeoutPage />;
}
// If the language data does not exist or is loading show the preloader
if (!language && time.current < 10) {
return <Preloader />;
}
return (
<Router>
<NavBar />
<Switch>
<Route exact path="/" component={Home} />
<Route exact path="/hire" component={Hire} />
<Route exact path="/swimschool" component={SwimSchool} />
<Route exact path="/login" component={Login} />
<PrivateRoute path="/dashboard" component={DashBoard} roles={['any']} />
<Route exact path="*" component={PDNE} />
</Switch>
</Router>
);
};
It should be very simple i think but i just can't get it to work how it is supposed to, right now it will just show the preloader even after the 10 seconds have passed, am i doing something wrong that i am just overlooking?
getLangis time consuming?!