I am calling one function through setInterval
but function go to infinite loop and my applicaiton get stuck. It’s happen it on android, Could someone please help me how to resolve this issue.
Thanks
useEffect(() => {
const interval = setInterval(() => {
sendCoordinate(location);
}, 180000);
return () => clearInterval(interval);
}, [location]);
A function that I am calling
const sendCoordinate = async (locParms) => {
console.log("@2 it working3", checkDayStatus, location);
if (checkDayStatus === "Started") {
let data = {
lat:
locParms !== null
? locParms && locParms.lat
: location && location.lat,
lng:
locParms !== null
? locParms && locParms.lng
: location && location.lng,
dispatcher_id: 1,
truck_id: 1,
};
return await LocationAPI.sendLocationFeed(data);
}
};
2
Answers
It turns out that your timer is created every time, just leave the useEffect arguments empty, [location] -> []
So you need to change the location from the dependency array like this.