0

I'm creating a page where it parses data from a json feed, but I wan't it to update every 30 seconds only refreshing the Div & the Jquery not actually refreshing the page.

I've tried a few things I've found on here but none have worked for me, The only thing that was happening is it was making page page go to infinite loop

Code:

<script type='text/javascript'>
var username = "mrsyndicate";

$(window).load(function(){
$.getJSON("http://imraising.com/"+ username +"/json/livedata.json", function (data) {
    $.each(data.donation, function (index, item) {
        $("<div>").html('<font color="950202"><b>'+ item.screen + '</b></font>&nbsp;&nbsp;$' + item.amount).appendTo("#content");
        $("<div>").html(item.comment + '<br /><br />').appendTo("#content");
    });
});
});
</script>

And it loads on the page with

<div id="content"></div>

I've got it working all printing out the way I wan't it, only I Cannot get refresh working, Any advice is welcomed!

2 Answers 2

3

Turn it into a function that you can call:

$(window).load(function() {
    function getFeed() {            
        var username = "mrsyndicate";
        $.getJSON("http://imraising.com/"+ username +"/json/livedata.json", function (data) {
            $("#content").html("");
            $.each(data.donation, function (index, item) {
                $("<div>").html('<font color="950202"><b>'+ item.screen + '</b></font>&nbsp;&nbsp;$' + item.amount).appendTo("#content");
                $("<div>").html(item.comment + '<br /><br />').appendTo("#content");
            });
        });
    }
    getFeed();
    setInterval(getFeed, 30000);
});
Sign up to request clarification or add additional context in comments.

3 Comments

won't that build-up repeated items? it probably needs a $("#content").html("") at the top...
You are correct. Thanks for the heads up. I fixed it.
I was coming back after testing it to say that exactly what dandavis said! Thanks! updated the code on my end testing it now!
0

See setInterval(), it acts like a loop but you can change the interval so it doesn't crash even if it's infinite

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.