TheWebMann
TheWebMann

Reputation: 115

How do I add a setinterval to this getJson

Hi there I am looking to set a setinterval of 30 seconds to my getJson function any hints would be graciously received ...

My code is as follows ...

  $(function() 
{

$(document).ready(function()
 {



    $.getJSON("tempdata.json",function(data)
    {
            $.each(data.recent, function(i,data){
                var div_data ="<div class='row'><div class='col-sm-12   rowpost'><div class='col-sm-3'><div class='postplace'><img src='"+data.coverurl+"' width='80' height='50'></img></div></div><div class='col-sm-9'>  <span class='post_title1'>"+data.trackartist+"</span><br>  <small1><em>"+data.tracktitle+"</em></small1></div></div></div>";

                $(div_data).appendTo("#Tracksinner");
            });
        }
    );
    return false;
});


});

Upvotes: 0

Views: 3134

Answers (3)

Sushanth --
Sushanth --

Reputation: 55750

var timer;

timer = setInterval(function() {

  // Your get JSON call

}, 30 * 1000);

Upvotes: 1

Daniel
Daniel

Reputation: 4946

You will need to ecapsulate your function to fetch your data on each interval.

To make an intial run you call the function update_trackdata() After that the interval will take over.

  $(document).ready(function() {

    function update_trackdata() {
            $.getJSON("tempdata.json", function(data) {
                var div_data = "";
                $.each(data.recent, function(i, data) {
                    div_data += "<div class='row'><div class='col-sm-12   rowpost'><div class='col-sm-3'><div class='postplace'><img src='" + data.coverurl + "' width='80' height='50'></img></div></div><div class='col-sm-9'>  <span class='post_title1'>" + data.trackartist + "</span><br>  <small1><em>" + data.tracktitle + "</em></small1></div></div></div>";
                    ("#Tracksinner");
                });
                $("#Tracksinner").html(div_data);
            });
            return false;
    }

    update_trackdata();

    var interval = setInterval(update_trackdata, 30000);

});

Upvotes: 3

CMedina
CMedina

Reputation: 4222

use setTimeout for wait .....

Try

$.getJSON("tempdata.json", function (data) {

    setTimeout(function () {
        $.each(data.recent, function (i, data) {
            var div_data = "<div class='row'><div class='col-sm-12   rowpost'><div class='col-sm-3'><div class='postplace'><img src='" + data.coverurl + "' width='80' height='50'></img></div></div><div class='col-sm-9'>  <span class='post_title1'>" + data.trackartist + "</span><br>  <small1><em>" + data.tracktitle + "</em></small1></div></div></div>";

            $(div_data).appendTo("#Tracksinner");
        });
    }, 30000);

});

Upvotes: 1

Related Questions