How to check if a JSON object value has changed wh

2019-09-08 20:07发布

问题:

I have a php script that gets the latest tweet of particular users in JSON format. The JSON is parsed into an AJAX function that displays the latest tweet in an ID on the page. A timer is set for every x seconds to execute the above, retrieving the latest tweet without the user having to refresh the page. This works perfectly.

I am now trying to create a notification feature that displays a notification when there is a new tweet to display. Each tweet has a unique ID called 'id_string' in the JSON retrieved. What I want to do is somehow store the generated id_string's value, and then each time a request is made to retrieve a new tweet, check the new 'id_string' against the stored one, and if it is different then display the notification.

Any ideas on how to go about this would be great?! I've looked into local storage but I'm not very familiar with this, and as far as I'm aware you can't check strings against other strings in local storage.

Here is the necessary code to help with interpreting my question:

PHP to make request to twitter and generate output in JSON format (tweets.php):

require_once('config/FrameFunctions.php');

$output = array();

foreach ($tweeters as $i => $tweeter){
    $theTweeter = new Tweeter($tweeter, $tmhOAuth);
    $allinfo = $theTweeter->getTweets();
    $output[$i] = $allinfo;
}

header("Content-type: application/json");
echo json_encode($output);

The javascript to make the request every x seconds and generate the client output:

$(document).ready(function() {

   $('.fancybox').fancybox();
   /*
   *  call ajax function and update latest
   */        

    var refreshTweets = function() {
      console.log("updating..");
      $.ajax({url:"tweets.php",success:function(result){
        tweets = eval(result);

        for(i=0;i<tweets.length;i++){
            $("#latesttweet"+(i+1)).html(
               tweets[i][0].user.name + ": " + tweets[i][0].text
             );  
        }

      }});

    }
    refreshTweets();

    //set the time in milliseconds here for each refresh
    setInterval(refreshTweets , 30000); //Interval

});

回答1:

here is a solution based on my comments above

/*
NOTE : allTweets will have to be populated with all the tweets ids on the page prior           
to being called in refreshTweets so that it does not trigger fake notifications
*/

    var allTweets = Array();

    $(document).ready(function() {

        $('.fancybox').fancybox();
        /*
         *  call ajax function and update latest
         */        

        var refreshTweets = function() {
            console.log("updating..");
            $.ajax({url:"tweets.php",success:function(result){
                    tweets = result;

                    for(i=0;i<tweets.length;i++) {

                        //check if its a new tweet or not
                        if (allTweets[tweets[i][0].id_string] === undefined) {
                            allTweets[tweets[i][0].id_string] = 1;    
                            //trigger notification here!
                        }

                        $("#latesttweet"+(i+1)).html(
                        tweets[i][0].user.name + ": " + tweets[i][0].text

                    );  
                    }

                }});

        }
        refreshTweets();

        //set the time in milliseconds here for each refresh
        setInterval(refreshTweets , 30000); //Interval

    });


回答2:

I want to comment inline with the others, but I guess I'm too new.

Anyways, if your not storing sensitive information you can store your data in JSON which can be quickly written to and read by PHP for smaller files. If you're storing a lot of data, you might want to look at saving values to a MySQL database or something similar.

Setting up both JSON storage and MySQL databases seem to be really well documented here.