javascript代码首先使用setTimeout工作,但然后崩溃或不起作用?

时间:2012-06-11 11:00:11

标签: javascript ajax facebook api 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,

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

php代码:

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

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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);
         }
      });

答案 2 :(得分: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 here的更多内容可以阅读 您还应该阅读setTimeout here

的机制