0

I'm having issues comparing current list items on a page with ones that are retrieved through a PHP AJAX refresh. I only want the box to refresh if there are new items.

The PHP returns a bunch of formatted, HTML list items.

<ul class="socialFeed twitterFeed">
    <script type="text/javascript">
        $(function() {
            refreshTweets();
        });
        function refreshTweets() {
            $.get("php/tweets.php", function(data){
                var current = $(".socialFeed.twitterFeed").html();
                if(current != data) {
                    $(".socialFeed.twitterFeed").hide().html(data).fadeIn();
                }
            });
        }
        setInterval("refreshTweets()", 2000);
    </script>
</ul>

I can't figure out why the string comparison isn't working. I'm missing something, but I'm not sure what.

6
  • What do console_log's of data and current give you? You'd better save an old version of data to compare against. Apart from that I would try to modify the php to save a timestamp (in for example a session) so that you can use that to get only the tweets since that time. Commented Jan 8, 2013 at 20:54
  • There is no guarantee that the html generated from the dom will be the same as the html the dom was built from. Commented Jan 8, 2013 at 20:54
  • Can you explain what you mean, Musa? Commented Jan 8, 2013 at 20:55
  • You will need to change the way you comparing your data. Ins't these tweets have any identifier? Commented Jan 8, 2013 at 20:55
  • Check jsfiddle.net/mowglisanu/39me6 Commented Jan 8, 2013 at 21:01

1 Answer 1

2

Try this:

<ul class="socialFeed twitterFeed">
    <script type="text/javascript">
        var current;
        $(function() {
            refreshTweets();
        });
        function refreshTweets() {
            $.get("php/tweets.php", function(data){
                if(current != data) {
                    current = data;
                    $(".socialFeed.twitterFeed").hide().html(data).fadeIn();
                }
            });
        }
        setInterval(refreshTweets, 2000);
    </script>
</ul>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.