I am trying to cast some data to state on an interval, I am trying to make a graph that updates every x seconds.
fuction func() {
console.log('Hello, world!')
}
setInterval(func, 5000)
Instead of responding every 5 seconds as expected I get two responses every 10 seconds, this problem gets far worse when i try and cast to state as I actually want to.
Here is the response from my API call, I all I am trying to do is cast it to state and log the result.
{
"_id": "60d5e8a81a68bb7b14d9f3b0",
"symbol": "LTC",
"perc": "3.55",
"createdAt": "2021-06-25T14:31:04.677Z",
"updatedAt": "2021-06-25T14:31:04.677Z",
"__v": 0
}
When I use the below code, instead of a double result in double the time, it goes absolutes haywire.
let [ltcArb, setLtcArb] = useState([]);
const fetchLtcArbHandler = () => {
fetch("/arb/ltc")
.then((res) => res.json())
.then((json) => setLtcArb((prev) => [{ perc: json.perc, time: json.createdAt },...prev,]))
.then(console.log(ltcArb));
};
function poes() {
console.log("poes");
}
setInterval(fetchLtcArbHandler, 5000);
I have been knocking my head at this for hours and quite honestly lost some sleep last night. Any and all help will be appreciated, take it easy on me please I am still very very new here. Thanks