On stackoverflow I assume the stackexchange notifications are being reloaded with jquery.load() or jquery.ajax() every 10-15 seconds.
However when I tested this, every few times it returned an error within the google chrome console.
So is there a way to optimize this so it will work everytime? And is it a good idea to pressure the database every 10-15 seconds like stackoverflow does to reload notifications?
This is my function
function reloadnotifications() {
a1 = $("#msgrfrsh");
a2 = $("#mybadrfrsh");
a3 = $("#yrpgrfrsh");
$.ajax({
url: '/notify.php?a',
cache: 'false',
success: function(response){
a1.html(response)
}
});
$.ajax({
url: '/notify.php?b',
cache: 'false',
success: function(response) {
a2.html(response)
}
});
$.ajax({
url: '/notify.php?c',
cache: 'false',
success: function(response) {
a3.html(response)
}
});
n = notifications();
if (n>0) {
//n is past zero, new notifications needed.
if ($("#newnotifications").css('opacity') == 1) {
//opacity is equal to 1, so refresh IF n is higher than current value.
if (n > parseInt($("#newnotifications").html())) {
$('#newnotifications').css({'opacity':'0','margin-top': '-55px'})
.html(notifications())
.animate({'margin-top': '-16px','opacity':'1'});
} else {
// leave it for now
}
} else {
//notifications is already invisible, so fade it in. IF the members bar height is 28px
if ($("#chat").height() == '28px') {
$('#newnotifications').css({'opacity':'0','margin-top': '-55px'})
.html(notifications())
.animate({'margin-top': '-16px','opacity':'1'});
} else{
//leave it for now
alert("we got here");
}
}
} else {
//notifications have all been read, so a fade out is neccessary.
$('#newnotifications').animate({'opacity':'0','margin-top': '-55px'});
}
//thats the end!
}
setInterval("reloadnotifications()",15000);