AJAX jQuery实时评论


                    

我一直试图让我的网站使用带有jQuery的AJAX来进行实时评论(这样评论就出现了,而无需刷新页面)(其余代码在PHP和HTML中)。 这是我一直在使用的代码,但是它似乎并不想工作-comments.php是显示注释的文件,$ comments是注释的变量。</ p>

  <br>
     var int = self.setInterval(“ showComments()”,5000);<br>
     函数showComments(){<br>
         $ .post(“ ajax_comments.php”,function(data){<br>
         $(“#comments).html(data);<br>
           });<br>
     }<br>
 &lt;/ script&gt;<br>
 &lt;/ code&gt; &lt;/ pre&gt;<br>
     &lt;/ div&gt;</p>

展开原文

原文

I've been trying to get my website to do live comments (so the comments appear without refreshing the page) using AJAX with jQuery (the rest of the code being in PHP and HTML). This is the code I have been using, however it doesen't seem to want to work - comments.php is the file which displays the comments, the $comments being variable for the comments.

<script type="text/javascript">
    var int=self.setInterval("showComments()",5000);
    function showComments(){
        $.post( "ajax_comments.php", function( data ) {
        $("#comments).html( data );
          });
    }
</script>

1个回答


  <br>
     var int = self.setInterval(showComments,5000);<br>
     函数showComments(){<br>
         $ .post(“ ajax_comments.php”,function(data){<br>
           $(“#comments”)。html(data);<br>
         });<br>
     }<br>
 &lt;/ script&gt;<br>
 &lt;/ code&gt; &lt;/ pre&gt;</p>

<p><p> <a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval" rel="nofollow noreferrer"> <a href="https://developer.mozilla.org/en">https://developer.mozilla.org/en</a>  -US / docs / Web / API / WindowOrWorkerGlobalScope / setInterval </a> &lt;/ p&gt;</p>

<p><p>在setInterval函数中,第一个参数应该是对该函数的引用,而不是带有该函数名称的字符串。 该字符串是使用eval()函数执行的,不建议这样做,即使在此处可能不会出现问题,也最好避免这样做。  &lt;/ p&gt;<br>
     &lt;/ div&gt;</p>

展开原文

原文

<script type="text/javascript">
    var int=self.setInterval(showComments,5000);
    function showComments() {
        $.post( "ajax_comments.php", function( data ) {
          $("#comments").html( data );
        });
    }
</script>

https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval

In the setInterval function the first parameter should be a reference to the function, not a string with the name of the function. The string gets executed using the eval() function, which is not recommended and even though it might not be a problem here it is simply best to avoid.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问