@nakoyawilson
Posted
Instead of having location.reload();
in the randomFunction()
function, you can include the fetch code directly but you would need to call the function once so that there is data when the page initially loads. Then on each click data will be fetched from the API.
function randomFunction() {
fetch("https://api.adviceslip.com/advice")
.then((data) => data.json())
.then((advice) => generateAdvice(advice));
}
randomFunction();
Also, to maintain separation of concerns, I would suggest removing the onclick="randomFunction()
from index.html and adding an event listener to dice from the script.js file.
Marked as helpful
@unexunderscore
Posted
@nakoyawilson
I changed the code, but it doesn't work in firefox browser until I add to fetch the option cache: "no-store"
.
Without it I always get the same data from fetch.
Thank you for your help! Have a nice day!
@nakoyawilson
Posted
@unexunderscore You're welcome and glad you were able to find a solution that worked for you. You made me realise that my solution also wasn't working in Firefox so thanks for pointing that out.