I'm fetching emails via Gmail API and storing some details like: message snippet, date, sender etc and showing a list on page using that data. I'm successfully getting data from API and storing in DB. But data appears on page only when request is finished and page got reloaded. I want to make it that way that as emails are getting entered into DB, they should appear on page. No matter request is completed yet or not, but DOM should get updated as data coming into DB. My Ajax call looks something like this.
$.ajax({
method: 'post',
url:'refreshMailBox.php',
data:{userId:'<?php echo $email;?>'},
beforeSend: function () {
$("#refreshMsg").html("<img src='images/ajax-loader.gif' alt='...'> <b>Loading...</b>");
},
success: function (response) {
res_obj = JSON.parse(response);
$("#refreshMsg").html("");
totalEmails = res_obj.totalEmails;
var url = window.location.href;
window.history.pushState("", "", url);
location.href = url;
},
error: function (err) {
console.log(err);
},
});
Please give me idea about how can I update DOM as data enters into DB. it doesn't matter ajax call completed or not. We should be seeing in DB data to check if new emails came then add them in listing div. Thanks