I have a javascript function that looks like this:
function getAlerts() {
$.post('getAlerts.php', function(data) {
//Do stuff
setTimeout(getAlerts, 1000);
});
}
I then have in the document.ready block:
$(document).ready(function() {
setTimeout(getAlerts, 1000);
}
I want the getAlerts function to be called every second, starting one second after the page has loaded. However, when I add this callback to the document.ready block, it causes the entire page to load extremely slowly, if at all. What am I missing here?
EDIT: I also notice that the slow page load only starts after enabling the getAlerts function, and refreshing the page a couple times. The first few refreshes seem fine. Is it possibly that the AJAX calls are bottlenecking somewhere?
I would try taking out the setTimeout, it’s delaying the function by a second. Also you may want to take a look at the php code that you are posting to. If it is making tons of mysql queries or something that could be slowing your load time as well.