skip to Main Content

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


  1. It turns out that your timer is created every time, just leave the useEffect arguments empty, [location] -> []

    Login or Signup to reply.
  2. useEffect(() => {
      const interval = setInterval(() => {
        sendCoordinate(location);
      }, 180000);
    
      return () => clearInterval(interval);
    }, [location.lng, location.lat]);
    

    So you need to change the location from the dependency array like this.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search