I have a script that calls from json data to display who is on a webpage at a given time. I have a function that is called at the very bottom of my page once. There is then a timer that calls it every 5 seconds.
When the code initially runs, Nothing happens. The alert(visListHtml);
is empty. But when it runs on the timer, tt works fine.
Any idea what is causing this to be empty on its very first run? Google DEV tools show data being returned by the getJSON. If I put an alert in the for loop. It pops up populated fine each time.
It's just the first time it runs, it does nothing. visListHtml
is empty.
I have the following code:
var prev_GetLiveVisitorList = "";
var visListHtml = '';
function GetLiveVisitorList() {
$.getJSON('<?php echo $apiEndpoint; ?>?cmd=GetLiveVisitorList&rnd'+Math.random(), function (allStatusJSON) {
for (i in allStatusJSON.websiteVisitor) {
visipAddr = allStatusJSON.websiteVisitor[i].visipAddr;
vistime = allStatusJSON.websiteVisitor[i].vistime;
visLandingPage = allStatusJSON.websiteVisitor[i].visLandingPage;
visRefPage = allStatusJSON.websiteVisitor[i].visRefPage;
visListHtml += '<div class="media"><span class="avatar status-success"><img src="../assets/img/avatar/1.jpg" alt="..."></span><div class="media-body"><p><strong>'+visipAddr+'</strong><time class="float-right" datetime="2018-07-14 20:00">'+vistime+'</time></p><p class="fs-14 semibold">Landing page: '+visLandingPage+'</p><p>Reffered By: '+visRefPage+'</p></div></div>';
}
});
alert(visListHtml);
if (prev_GetLiveVisitorList != visListHtml) {
document.getElementById("GetLiveVisitorList").innerHTML = visListHtml;
prev_GetLiveVisitorList = visListHtml;
}
}