Design comparison
Solution retrospective
I had some issues using the API in this project. By using the console, I can see that my code is fetching the data when the page loads or the dice icon is clicked, but the advice fetched only changes once every few minutes, and I'm lost as to why that is. Any advice would be appreciated.
Community feedback
- @RoksolanaVeresPosted about 1 year ago
Hi, Natalie! I have the answer to your question. The problem is in the very API, it takes a different amount of time to fetch data depending on the browser. And if you can generate a new piece of advice in Chrome every 2 seconds, in Firefox, for example, it takes an eternity to get another advice. One of the ways to overcome this issue is to modify your fetch request in this way:
var response = await fetch("https://api.adviceslip.com/advice", { method: "GET", mode: "cors", cache: "no-cache", });
Hope it helps and happy coding 🙂
Marked as helpful1 - @Zy8712Posted about 1 year ago
Your site looks pretty good. Not too sure what you mean by "advice fetched only changes once every few minutes", as I tested your site and it works as intended.
One thing worth mentioning though is that its stated on the AdviceSlipAPI website that: "Advice is cached for 2 seconds. Any repeat-request within 2 seconds will return the same piece of advice." This might be what you mean by advice only changes after x amount of time.
Hope this helps 👍
Marked as helpful1@nataliesmythPosted about 1 year ago@Zy8712 I just tested it in my phone and it is working properly….. but earlier when I tested it locally I could tell that I was getting a response from the api from the console, but the response was the same advice over and over again. This is my first project using an API, so I’m still figuring out how to articulate what my issues are, so apologies for likely not explaining myself properly. Ps it’s working and that’s so exciting! Cheers!
0
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord