Adding a Loading Spinner to Improve User Experience

Day 8 #100DaysOfCode 💻 Today I learned how to add a loading spinner before fetching API data. When we fetch data from an API, it may take some time. To improve user experience, we can show a loading spinner while the data is loading and hide it after the data is received. Simple example: function showLoading(){ document.getElementById("spinner").classList.remove("hidden"); } function hideLoading(){ document.getElementById("spinner").classList.add("hidden"); } showLoading(); fetch("https://lnkd.in/gAwJs9UG") .then(res => res.json()) .then(data => { console.log(data); hideLoading(); }) .catch(error => { console.log(error); hideLoading(); }); Small improvement, but it makes the UI feel much more professional. #100DaysOfCode #JavaScript #WebDevelopment #FrontendDevelopment #API #LearningInPublic #Akbiplob

To view or add a comment, sign in

Explore content categories