Skip to content Skip to sidebar Skip to footer

How To Check If A Json Object Value Has Changed When New Request Is Captured

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 t

Solution 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 notif (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 refreshsetInterval(refreshTweets , 30000); //Interval

    });

Solution 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.

Post a Comment for "How To Check If A Json Object Value Has Changed When New Request Is Captured"