0

I've tried to use setinterval to allow my ajax request to send every 2 seconds but it keeps crashing the page, So I think something is going wrong!

Here is my code:

 var fburl = "http://graph.facebook.com/http://xzenweb.co.uk?callback=?";

        //getting facebook api content
        $.getJSON(fburl, function(data){

        var name = data["shares"];
        var dataString = 'shares='+name;

        //sending share count data to server
        $.ajax({
        type: "POST",
        url: "index.php",
        data: dataString,
        cache: false,

        success: function(html)
        {
        $("#content").html(html);
        }
    });
return false;   
}); 

I'm new to both ajax and javascript, would really appreciate it if you could help me out :)

Andrew Charlton
  • 251
  • 1
  • 7
  • 20
  • well I made get.JSON request to Facebook to retrieve the information I wanted then sent it via ajax via $.ajax – Andrew Charlton Jun 11 '12 at 09:35
  • Please provide the `setinterval` part. It's rather hard to crash the page. You probably have an infinite loop somewhere. – Konrad Dzwinel Jun 11 '12 at 09:35
  • Thanks Konrad - I know haha I feel like such a noob. – Andrew Charlton Jun 11 '12 at 09:38
  • Here was the set interval code: setInterval(function() {//getting facebook api content $.getJSON(fburl, function(data){ var name = data["shares"]; var dataString = 'shares='+name; //sending share count data to server $.ajax({ type: "POST", url: "index.php", data: dataString, cache: false, success: function(html) { $("#content").html(html); } }); return false; }); – Andrew Charlton Jun 11 '12 at 09:40
  • and at the bottom of that: }, 2000); – Andrew Charlton Jun 11 '12 at 09:41

1 Answers1

1

Provide a callback function to the $.getJson

function test(){
         $.getJSON(fburl, 
              function(data) {
                  //your method
              }); 
         setInterval("test()",2000);
      }

UPDATED ANSWER ::

<script>

$(document).ready(function(){
    test();
  });


function test(){
    $.getJSON("http://graph.facebook.com/http://xzenweb.co.uk?callback=?", 
         function(data) {
         var name = data["shares"];
            var dataString = 'shares='+name;

            $.ajax({
                type: "POST",
                url: "index.php",
                data: dataString,
                cache: false,

                success: function(html)
                {
                $("#content").html(html);
                }
            });
            return false;  
         }); 
    setTimeout("test()",5000);
 }




</script>


<body>
<div id="content">Hello</div>
</body>
Ashish Gupta
  • 1,651
  • 14
  • 30