我怎么能继续重发这个ajax请求?

时间:2012-06-11 09:27:37

标签: javascript ajax json setinterval

我尝试使用setinterval允许我的ajax请求每2秒发送一次,但它会不断崩溃页面,所以我觉得有些事情出错!

这是我的代码:

 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;   
}); 

我是ajax和javascript的新手,如果你能帮助我,我会非常感激:)

1 个答案:

答案 0 :(得分:1)

为$ .getJson

提供回调函数
function test(){
         $.getJSON(fburl, 
              function(data) {
                  //your method
              }); 
         setInterval("test()",2000);
      }

更新答案::

<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>