1

在 stackoverflow 上,我假设每 10-15 秒使用 jquery.load() 或 jquery.ajax() 重新加载 stackexchange 通知。
但是,当我对此进行测试时,每隔几次它就会在 google chrome 控制台中返回一个错误。

那么有没有办法优化它,让它每次都能工作?每隔 10-15 秒对数据库施加压力是不是一个好主意,就像 stackoverflow 那样重新加载通知?

这是我的功能

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);
4

0 回答 0