简体   繁体   English

AJAX jQuery 实时评论

[英]AJAX jQuery live comments

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).我一直在尝试使用带有 jQ​​uery 的 AJAX(其余代码使用 PHP 和 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.这是我一直在使用的代码,但它似乎并不想工作 - comments.php 是显示评论的文件,$comments 是评论的变量。

<script type="text/javascript">
    var int=self.setInterval("showComments()",5000);
    function showComments(){
        $.post( "ajax_comments.php", function( data ) {
        $("#comments).html( data );
          });
    }
</script>
<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 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.在 setInterval 函数中,第一个参数应该是对函数的引用,而不是带有函数名称的字符串。 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.字符串使用 eval() 函数执行,这是不推荐的,即使在这里可能不是问题,也最好避免。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM