4

我的代码在最初的几分钟内工作,然后我的页面崩溃或之后它不起作用 - 它有时还会说我的内容应该在哪里“未定义”,我的代码有什么问题?

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

php代码:

if(isset($_POST["shares"])) {
    echo $_POST["shares"];
} 
4

3 回答 3

0

或者,使用 .done、.fail 和 .always 可能是个好主意。并在 .always 中执行 setTimeout(无论请求是成功 - 完成,还是错误 - 失败。)

$(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
      })
      .done(function(html) { $("#content").html(html); })
      .always(function() { setTimeout(test,5000); })
   }); 
 }

或者在 ajax 内部,您可以使用 complete 设置超时(以确保即使发生错误也会调用它),如下所示:

$.ajax({
         type: "POST",
         url: "index.php",
         data: dataString,
         cache: false,
         success: function(html) {
           $("#content").html(html);
         },
         complete: function() {
           setTimeout(test,5000);
         }
      });
于 2012-06-11T11:44:09.663 回答
0

试试这个 - 注意我只在成功运行后调用超时:

另请注意,您可能会遇到允许的通话次数限制

$(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);
           setTimeout(test,5000);
         }
      });
   }); 
 }

这是对 json 循环的测试,直到数量更改或 10 次调用(因为用户可能已经共享)

<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf8-8">
  <title></title>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  <script>
  var savedData ="";
  var cnt = 10;
  $(document).ready(function(){
  test();
});

function test(){
  $.getJSON("http://graph.facebook.com/http://xzenweb.co.uk?callback=?", 
     function(data) {
       var shares = data["shares"];
// change this line to 
// $("#content").html(shares);
// when you are happy
       $("#content").html(cnt + ":"+shares+" - "+new Date());
       if (cnt > 0) {
         if (savedData == "" || savedData == shares ) {
           setTimeout(test,1000);
         }
       }
       savedData = shares;
       cnt--
  });
}

  </script>
  </head>
  <body>
<div id="content"></div>
  </body>
</html>
于 2012-06-11T11:20:10.543 回答
0

这是我从上面的评论中建议的代码。

var timeout; // create a variable named `timeout` assign nothing to it.

$(document).ready(function(){ // I really don't see the point in $(document).ready, if you include your code in the body of the document this should not be needed.
    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);
                },
                complete: function(xhr, status) { // I run after success and error callbacks have fired.
                   clearTimeout(timeout); // I clear any timeouts assigned to timeout variable. (these are denoted with an integer ID)
                   timeout = setTimeout(test , 5000); // knowing there is no longer a timeout waiting to fire, I can re-assign a timeout to the variable.
                }
            });
            return false;  
         }); 
}

我已经对代码进行了更多评论,以帮助澄清正在发生的事情。

可以在此处
阅读有关 jQuery.ajax 的更多信息 您还应该在此处阅读 setTimeout 的机制

于 2012-06-11T11:49:49.213 回答