-1

I am trying to reload data every 5 seconds using jQuery. The URL for the JSON data can be found at http://gdx.mlb.com/components/game/win/year_2015/month_11/day_11/master_scoreboard.json

This is the code I am trying:

$.getJSON("http://gdx.mlb.com/components/game/win/year_2015/month_11/day_11/master_scoreboard.json", function (json) {
    $.each(json.data.games.game, function (i, value) {

        $('#LMP').append('<div id="equipo"><div class="p1"><img src="img/lmp/' + value.away_name_abbrev + '.png' + '" alt=""></div><div class="p2"><div class="p2-1"> </div><div class="p2-2">' + value.status.inning + ' ' + value.status.top_inning + '</div><div class="clear"></div></div><div class="p3"><img src="img/lmp/' + value.home_name_abbrev + '.png' + '" alt=""></div><div class="clear"></div></div>');

    });
});
xyhhx
  • 6,384
  • 6
  • 41
  • 64
  • 1
    can you explain in little more detail? What exactly are you trying to achieve – Rajesh Nov 12 '15 at 06:12
  • That information recharged every 5 seconds to refresh the json information. – David Emanuel Nov 12 '15 at 06:14
  • So you can try to enclose this call in a function and call this function using a `setInterval(function, timeInterval)`. – Rajesh Nov 12 '15 at 06:16
  • I can put it like ? I am new to javascript – David Emanuel Nov 12 '15 at 06:18
  • you can check this [post](http://stackoverflow.com/questions/729921/settimeout-or-setinterval) for reference. Also refer [setInterval - MDN](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval) and [setTimeout - MDN](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout). – Rajesh Nov 12 '15 at 06:29

1 Answers1

0

One way to do it might look like this:

function getJson () {
    $.getJSON("http://gdx.mlb.com/components/game/win/year_2015/month_11/day_11/master_scoreboard.json", function (json) {
        $.each(json.data.games.game, function (i, value) {

            $('#LMP').append('<div id="equipo"><div class="p1"><img src="img/lmp/' + value.away_name_abbrev + '.png' + '" alt=""></div><div class="p2"><div class="p2-1"> </div><div class="p2-2">' + value.status.inning + ' ' + value.status.top_inning + '</div><div class="clear"></div></div><div class="p3"><img src="img/lmp/' + value.home_name_abbrev + '.png' + '" alt=""></div><div class="clear"></div></div>');

        });
    });
}

var i = setInteval(getJson, 5000);
xyhhx
  • 6,384
  • 6
  • 41
  • 64