正在获取未读邮件计数

Getting UnreadmessagesCount

本文关键字:获取      更新时间:2023-09-26

我在这里遇到的问题是,出于某种原因,当运行getInboxUnreadMessagesCount js函数时,它会出现一个不同的数字,然后是什么,请记住,没有发送新消息。当我运行php仪表板函数时,它们都返回了正确的数字,但我认为问题在于消息Timer 的最后一行代码

有人想过它可能是什么吗?我希望有人能弄清楚。

var $messageCountJSON;
var messageTimer = '';
var messageInterval = 5;
//assumed JSON response is {"count":"20"} for example sake.
function getInboxUnreadMessagesCount(displayElementID) {
    $.get('dashboard/getInboxUnreadMessagesCount', function (data) {
        $messageCountJSON = data;
    }, 'json');
    if (displayElementID != null && displayElementID != undefined && displayElementID != '') {
        //$('#'+displayElementID).html($messageCountJSON);
        if (parseInt($('#' + displayElementID).text()) < parseInt($messageCountJSON)) {
            $.jGrowl("You have received a new private message!", { theme: 'information' });
            $('#' + displayElementID).html($messageCountJSON).css({ "display": "block" });
        }
        if (parseInt($messageCountJSON) == 0) {
            $('#' + displayElementID).html($messageCountJSON).css({ "display": "none" });
        }
    }
}
function getInboxMessagesCount(displayElementID) {
    $.get('dashboard/getInboxMessagesCount', function (data) {
        $messageCountJSON = data;
    }, 'json');
    if (displayElementID != null && displayElementID != undefined && displayElementID != '') {
        //$('#'+displayElementID).html($messageCountJSON);
        if (parseInt($('#' + displayElementID).text()) < parseInt($messageCountJSON)) {
            $('#' + displayElementID).html($messageCountJSON);
        }
        if (parseInt($messageCountJSON) == 0) {
            $('#' + displayElementID).html($messageCountJSON);
        }
    }
}
$(document).ready(function () {
    messageTimer = setInterval(function () { getInboxUnreadMessagesCount('notifications'); getInboxMessagesCount('inboxCount'); }, messageInterval * 1000);
});
//you can optionally kill the timed interval with something like
//$('#pmMessagesIcon').click(function(){clearInterval(messageTimer);})

您正试图在收到消息之前访问消息计数:

// Here you create an asynchronous request to the server.
 $.get('dashboard/getInboxUnreadMessagesCount', function (data) {
     // This section of your code will only run after you get the JSON response
    $messageCountJSON = data;
}, 'json');
// Code here will run immediately after the request is fired,
// and probably  before the JSON response arrives

您必须将大的if语句移动到每个$.get()回调函数内部。