0
function commentpost(){
  var allow = true;
  var game = "<?php echo $game ?>";
  var name = $("input#name").val();
  var comment = $("textarea#comment").val();
  var date =  currentdate();
  var dataString = "postdate=" + date + "&game=" + game + "&name=" + name + "&comment=" + comment;
  if(name && comment && allow){
    $.ajax({
      type: "POST",
      url: "../php/addcomment.php",
      data: dataString,
      success: function(){
        updatecomments();
        //important part here V
        allow = false;
        setTimeout(function(){
          allow = true; 
          // alert(allow);
        }, 5000);
        // alert(allow);
        //important part here ^
      }
    });
  }
  //important part here V
  else if ((!name || !comment) && allow){
    alert("Please fill out both the Name and Comment fields");
  }
  else if (!allow){
    alert("Commenting has been limited to 5 seconds in between to pevent spamming.");
  }
  //important part here ^
}

作成中の Web サイトに、小さくて基本的なコメント スクリプトがあります。それはすべて正常に機能しますが、送信ボタンがスパムにならないようにしています。これは私には機能するはずですが、何らかの理由で機能しないようです。「許可」の使い方が原因だと思います。

4

1 に答える 1

0

allow 変数にスコープの問題があります。新たに作成して、commentPost() が呼び出されるたびに true に設定すると、その値を確認すると常に true になります。

次のようなものが必要です。

var allow = true;

function commentpost(){
  //...
  if(name && comment && allow){
    $.ajax({
      type: "POST",
      url: "../php/addcomment.php",
      data: dataString,
      success: function(){
        updatecomments();
        allow = false;
        setTimeout(function(){
          allow = true; 
        }, 5000);
      }
    });
  }

もちろん、allow をグローバルに宣言することはお勧めしませんが、commentpost のコンテキストを知らなければ、どこに保存すべきかを判断するのは困難です。

于 2013-03-18T20:02:11.133 に答える