My Node API returns a JSON object, the object is being displayed on HTML table. However, my Node app takes some time to process and return the data. So till the time data has being retrieved I want a spinner to load on the page.
$(document).ready(function() {
$("#table1").hide();
$("#myButton").click(function() {
$("#myButton").hide();
$("#table1").show();
$.ajax({
url: "http://localhost:8090/api/route1",
type: 'POST',
data: {
data1: str1,
data2: str2
},
dataType: 'json',
success: function(res) {
console.log(res);
console.log("Result1", res.result1);
console.log("Result2", res.result2);
$('#td1').append(res.result1);
$('#td2').append(res.result2);
}
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class="spinner" id="loadingBtn">
Loading...
</span>
<table class="table" id="table1">
<tbody>
<tr>
<td id="td1"></td>
</tr>
<tr>
<td id="td2"></td>
</tr>
</tbody>
</table>
How and where do I put the spinner id so that it is only displayed until the data is fully rendered onto the HTML table.
4
Answers
You can just add the loading spinner on
loading
anddone
. Show the spinner on call, and just hide it when the call is done. like so:take a look at this codepen:
https://codepen.io/yic666kr/pen/mxmvbV
It depends on your structure where you have it placed but I’ll go with what you have put here. You have it visible when you start loading and hide it once data is loaded from your ajax.
Just show your spinner in
beforeSend
of ajax function and hide it after data is loaded undersuccess
function.