如何确保更新通知时jquery.load()始终有效?

时间:2013-10-15 14:45:21

标签: javascript jquery ajax

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

那么有没有一种方法可以优化它,以便每次都可以使用?并且像stackoverflow重新加载通知一样每隔10-15秒对数据库施加压力是一个好主意吗?

这是我的功能

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

0 个答案:

没有答案