From eb5487385ea506695cc6aae89c04cba832c5edff Mon Sep 17 00:00:00 2001 From: skidoodle Date: Sun, 9 Oct 2022 01:09:41 +0200 Subject: [PATCH] datahandler --- pages/index.tsx | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/pages/index.tsx b/pages/index.tsx index e21d28c..46cf5ef 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -12,18 +12,22 @@ import profilePic from "public/profile.webp"; import { useState } from "react"; export default function () { - const [spotify, setSpotify] = useState("nothing"); + const [spotify, setSpotify] = useState(""); + + const handleData = () => { + fetch("/api/spotify") + .then((res) => res.json()) + .then((data) => setSpotify(data)); + }; useEffect(() => { - const interval = setInterval(() => { - fetch("/api/spotify") - .then((res) => res.json()) - .then((data) => { - setSpotify(data); - }); - }, 9999); + handleData(); - return () => clearInterval(interval); + return () => { + setInterval(() => { + handleData(); + }, 3000); + }; }, []); return ( @@ -61,7 +65,7 @@ export default function () { {" "} - {spotify.song.title || "nothing"} + {spotify.song.title} ) : (