Advice Slip JSON API not working properly?

I'm doing the Frontend Mentor Challenge: Advice Generator which they say uses the API from the title. It says on their site that a new piece of advice can be fetched every 2 seconds. But when I click on my button to fetch my new advice it only gives me the same piece of advice for like several minutes and only then changing it up. I don't have any Network Errors only 200 Status returns after every button click. I don't think I have done anything wrong but the requested entry point also returns new advice after 2 seconds: https://api.adviceslip.com/advice
const adviceNumberEl = document.querySelector("#advice-id");
const adviceTextEl = document.querySelector("#advice-text");
const btn = document.querySelector("#btn");

async function getAdvice() {
try {
const res = await fetch("https://api.adviceslip.com/advice");
const data = await res.json();
adviceNumberEl.textContent = data.slip.id;
adviceTextEl.textContent = data.slip.advice;
} catch (error) {
console.log(error);
}
}

btn.addEventListener("click", getAdvice);
const adviceNumberEl = document.querySelector("#advice-id");
const adviceTextEl = document.querySelector("#advice-text");
const btn = document.querySelector("#btn");

async function getAdvice() {
try {
const res = await fetch("https://api.adviceslip.com/advice");
const data = await res.json();
adviceNumberEl.textContent = data.slip.id;
adviceTextEl.textContent = data.slip.advice;
} catch (error) {
console.log(error);
}
}

btn.addEventListener("click", getAdvice);
3 Replies
roelof
roelof•15mo ago
Think.about caching So now thr fetch is using a cache
lena_neko
lena_nekoOP•15mo ago
@roelof I looked around and found this. It seems to work now I just don't know if it's the correct way to do that. 😄
fetch (url, { cache: "no-store" });
fetch (url, { cache: "no-store" });
roelof
roelof•15mo ago
I used this one fetch("https://api.adviceslip.com/advice", { cache: "no-cache" }) but I think they are both doing the same
Want results from more Discord servers?
Add your server