php jQuery - 每 10 秒调用一次 ajax

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5687600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:12:13  来源:igfitidea点击:

jQuery - Call ajax every 10 seconds

phpjqueryajax

提问by ryryan

I have a mysql feedback database constructed like this:

我有一个这样构造的 mysql 反馈数据库:

name | location | feedback

Ryan | England | great support

姓名 | 位置 | 回馈

瑞安 | 英格兰 | 大力支持

Obviously there's more entries than that. I am trying to build a feedback div, where it displays a new feedback item every 10 seconds via ajax.

显然还有更多的条目。我正在尝试构建一个反馈 div,它通过 ajax 每 10 秒显示一个新的反馈项。

So I have constructed this:

所以我构建了这个:

$(document).ready(function(){
   new get_fb(); 
 });

function get_fb(){
var feedback = $.ajax({//Ajax
                        type: "POST",
                        url: "feedback.php",
                        async: false
                        }).responseText;//end of ajax

$('div.feedback-box').html(feedback).delay(10000).queue(function() {
    new get_fb(); 
    });
}

And here's my PHP file:

这是我的 PHP 文件:

$result = mysql_query("SELECT * FROM feedback ORDER BY RAND() LIMIT 0,1");
while($row = mysql_fetch_array($result))
{
    $name = $row['name'];
    $location = $row['location'];
    $feedback = $row['feedback'];

    echo "
    <p>Name: $name, Location: $location, Feedback: $feedback.</p>
    ";
} 

However, this only shows two. It doesn't keep showing new ones, it purely shows the first then the second and stops.

然而,这只显示了两个。它不会继续显示新的,它纯粹显示第一个然后第二个然后停止。

What am I doing wrong? Thanks :)

我究竟做错了什么?谢谢 :)

回答by Jared Farrish

Are you going to want to do a setInterval()?

你想做setInterval()吗?

setInterval(function(){get_fb();}, 10000);

Or:

或者:

setInterval(get_fb, 10000);

Or, if you want it to run only after successfully completing the call, you can set it up in your .ajax().success()callback:

或者,如果您希望它仅在成功完成调用后运行,您可以在.ajax().success()回调中设置它:

function get_fb(){
    var feedback = $.ajax({
        type: "POST",
        url: "feedback.php",
        async: false
    }).success(function(){
        setTimeout(function(){get_fb();}, 10000);
    }).responseText;

    $('div.feedback-box').html(feedback);
}

Or use .ajax().complete()if you want it to run regardless of result:

或者,.ajax().complete()如果您希望它运行而不管结果如何,请使用:

function get_fb(){
    var feedback = $.ajax({
        type: "POST",
        url: "feedback.php",
        async: false
    }).complete(function(){
        setTimeout(function(){get_fb();}, 10000);
    }).responseText;

    $('div.feedback-box').html(feedback);
}

Here is a demonstration of the two. Note, the success works only once because jsfiddle is returning a 404 error on the ajax call.

这是两者的演示。请注意,成功仅有效一次,因为 jsfiddle 在 ajax 调用中返回 404 错误。

http://jsfiddle.net/YXMPn/

http://jsfiddle.net/YXMPn/

回答by Muhammad Adeel Zahid

setInterval(function()
{ 
    $.ajax({
      type:"post",
      url:"myurl.html",
      datatype:"html",
      success:function(data)
      {
          //do something with response data
      }
    });
}, 10000);//time in milliseconds 

回答by Calum

You could try setInterval() instead:

你可以试试 setInterval() 代替:

var i = setInterval(function(){
   //Call ajax here
},10000)

回答by Feshibaba

This worked for me

这对我有用

setInterval(ajax_query, 10000);

function ajax_query(){
   //Call ajax here
}