JQuery - 仅当两次连续轮询的状态不同时才在 div 标记中增加计数器

JQuery - Increment counter in div tag only if two consecutive polls do not have the same status

我的 Javascript 中有一个轮询器 运行,它每 30 秒调用一个 PHP 文件。

PHP 文件 return 是每个间隔中的以下 JSON 之一。有时,它可以 return 相同的 JSON 和相同的 "status"。

ex: 
    {"message":"Action","status":"pending"}
    {"message":"Update","status":"requested"}
    {"message":"Request","status":"processing"}
    {"message":"Delete","status":"completed"}

如果在连续两次轮询中收到的状态字段中的数据不相同,我需要在 DIV 中更新一个计数器(就像你的通知气泡)

考虑这个应用程序流程:

        1. When page loads, the counter in div tag is empty. 
             a. Poller runs the first time after 30 seconds.
             b. Poller receives pending status.
             c. Increment the counter in div tag.

        2. The counter in div tag is 1 now.
            a. Poller runs the second time after 30 seconds.
            b. Poller receives "requested" status.
            c. Increment the counter in div tag because the status received now is 
               different than what was received in the previous request.

        3. The counter in div tag is 2 now.
            a. Poller runs the third time.
            b. Poller receives the same "requested" status.
            c. DO NOT Increment the counter, because the status received 
               in #2 is the same as in #3.

我该怎么做?

这是我目前在 OO JS 中的内容:

    var Poller = {

        //This function runs as as poller every 30 seconds.
        monitorStatus: function()
        {
            $.ajax({
                type: 'POST',
                url: "/getStatus.php",
                dataType: "json",
                success: function(data)
                {
                    if(data == null)
                    {
                      return false;
                    }
                var status = data.status;
                if(typeof status !== "undefined")
                {
                    if(status != "")
                    {
                        Poller.updateDivCounter(status.toLowerCase(), data.message);
                    }
                }
            }
        });
    },

    updateDivCounter: function(status, message)
    {
        //ADD CODE TO MAKE SURE WE UPDATE THE COUNTER ONLY IF THE STATUS IN TWO 
          CONSECUTIVE POLLS IS NOT THE SAME.
        //......Need help

        //Increment the notificationCount DIV Tag
        $(".notificationCount").text( function (i,current) { return +current+1;} ) ;

        //Print the notification in notificationMessage Div tag
        $(".notificationMessage").text(message);
    }

}

您只需要维护对收到的最后状态的引用:

var Poller = {

    //// monitorStatus function

    lastStatus: "Initialized", // just needs to something other than one of the actual statuses

    updateDivCounter: function(status, message)
    {
        if (status === this.lastStatus) {
             return;   // don't do anything if the statuses match
        }

        this.lastStatus = status;

        //Increment the notificationCount DIV Tag
        $(".notificationCount").text( function (i,current) { return +current+1;} ) ;

        //Print the notification in notificationMessage Div tag
        $(".notificationMessage").text(message);
    }

}