Responsive Advice Generator using HTML, CSS and JavaScript

Solution retrospective
Hi guys, I'm new to JavaScript so I would like to ask, Is there any other way to fetch new advice without reloading the page? Anyway, feel free to suggest anything from the design and the javascript part. Thanks and have a great day!
Please log in to post a comment
Log in with GitHubCommunity feedback
- @Ali-Akbar-Qasimi
first of all you did a great work
you can make a function that fetch the advice and on the dice listen for a click and run the function that fetch the advice , that way you are not suppose to reload the page , and at the first line of your code you can call that function so that the advice will be fetched at the first also.
getNewAdvice()
let dice = document.querySelector('.dice') let header = document.querySelector('.header') let advice = document.querySelector('.advice')
dice.addEventListener('click',()=>{ getNewAdvice() })
async function getNewAdvice(){ let response = await fetch('https://api.adviceslip.com/advice') let data = await response.json() console.log(data) header.textContent = 'Advice #' + data.slip.id advice.textContent = data.slip.advice }
this is how i made it, hope it helped you.
- @uttamsharma446
Hey JOHNROOKIE, Great work :) but while fetching data you can show loading content for eg.
let loading=false;
const fetchData =()=>{ loading=true fetch(api).then(response=>response()).then(result=>{ loading=false; //store data
}).catch(err=>{})
}
if(loading){ //show loading
} else{ show data content }
Note: this fetch data method you can use on click as well on initial load of the page
Join 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