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

これを試してください-実行が成功した後にのみタイムアウトを呼び出すことに注意してください:

また、許可される通話数に制限が生じる可能性があることにも注意してください

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

これは、番号が変更されるか、10回の呼び出しが行われるまでのjsonループのみのテストです(ユーザーがすでに共有している可能性があるため)

<!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 に答える
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 に答える