AJAX jQuery刷新div每5秒

我从一个网站上获得了这个代码,我根据自己的需要进行了修改:

     var timeout = setTimeout(reloadChat, 5000); function reloadChat () { $('#links').load('test.php #links',function () { $(this).unwrap(); timeout = setTimeout(reloadChat, 5000); }); }  

在test.php中:

  

所以我希望在链接div中每隔5秒调用一次test.php。 我怎么能这样做?

试试吧。

 function loadlink(){ $('#links').load('test.php',function () { $(this).unwrap(); }); } loadlink(); // This will run on page load setInterval(function(){ loadlink() // this will run after every 5 seconds }, 5000); 

希望这可以帮助。

尝试使用setInterval并包含jquery library然后尝试删除unwrap()

   

UPDATE

您使用的是旧版本的jquery,因此请包含最新的jquery版本

  
  

尽量不要使用setInterval
成功响应超时后,您可以向服务器重新发送请求。
jQuery的:

sendRequest(); //call function

 function sendRequest(){ $.ajax({ url: "test.php", success: function(result){ $('#links').text(result); //insert text of test.php into your div setTimeout(function(){ sendRequest(); //this will send request again and again; }, 5000); } }); } 

你可以用这个。

 

你的java脚本代码应该是这样的。

 setInterval(function(){ $('#test').load('test.php'); },5000);