JQuery - 仅当两个连续的轮询没有相同的状态时才在div标签中递增计数器

时间:2015-04-17 02:33:23

标签: javascript jquery counter

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

PHP文件在每个间隔中返回以下JSON之一。有时,它可以返回具有相同“状态”的相同JSON。

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

}

1 个答案:

答案 0 :(得分:2)

您只需要保持对上次收到状态的引用:

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

}