使用Javascript / ajax根据服务器时间和日期加载新数据

时间:2021-04-09 17:01:50

I currently have a JS ajax script to load data from the Database, where the query is in load.php How would I make auto load new content separately based on the server time and date? Such as rather than having the whole DB table refresh every "5 Seconds" But loading the current data once, then new data based on server time/date. Here is my script>

我目前有一个JS ajax脚本从数据库加载数据,其中查询在load.php中如何根据服务器时间和日期单独自动加载新内容?例如,而不是让整个数据库表每刷新一次“5秒”但是加载当前数据一次,然后根据服务器时间/日期新数据。这是我的剧本>

     <script language="JavaScript"> 
   window.onload = function() 
    { 
         $('#feed').empty(); 
         $('#feed').load("load.php"); 
    } 
</script> 

1 个解决方案

#1


You cannot initiate sending AJAX data from the server without a request from the client (browser). So you should use the first request (the browser requesting the page itself) to calculate the time offset on the server (in PHP), and put your .load() function inside a setTimeout which will execute that many seconds later. So your PHP file would look something like this:

如果没有来自客户端(浏览器)的请求,您无法从服务器发送AJAX数据。因此,您应该使用第一个请求(请求页面本身的浏览器)来计算服务器上的时间偏移量(在PHP中),并将.load()函数放在setTimeout中,这将在几秒钟后执行。所以你的PHP文件看起来像这样:

$time = 4300 // or whatever, based on your server clock.
echo '<script language="JavaScript">
      window.onload = function(){ 
         $("#feed").empty(); 

         /* if you want to execute it once at page load too:*/
         $("#feed").load("load.php"); 

         /* then load after $time milliseconds:*/
         setTimeout(function(){
             $("#feed").load("load.php");
         },'.$time.');
      }
      </script>';

Remember that setTimeout's offset is supposed to be in milliseconds.

请记住,setTimeout的偏移应该是以毫秒为单位。

Websockets are another solution to your problem if you have full server access (like to sockets and stuff). They involve basically creating a websocket connection when the script is first executed, and then server and client can send messages to each other as they please. These messages can include the data you wish.

如果您具有完整的服务器访问权限(如套接字和内容),Websockets是您的问题的另一种解决方案。它们涉及在首次执行脚本时基本上创建websocket连接,然后服务器和客户端可以随意发送消息。这些消息可以包含您希望的数据。

#1


You cannot initiate sending AJAX data from the server without a request from the client (browser). So you should use the first request (the browser requesting the page itself) to calculate the time offset on the server (in PHP), and put your .load() function inside a setTimeout which will execute that many seconds later. So your PHP file would look something like this:

如果没有来自客户端(浏览器)的请求,您无法从服务器发送AJAX数据。因此,您应该使用第一个请求(请求页面本身的浏览器)来计算服务器上的时间偏移量(在PHP中),并将.load()函数放在setTimeout中,这将在几秒钟后执行。所以你的PHP文件看起来像这样:

$time = 4300 // or whatever, based on your server clock.
echo '<script language="JavaScript">
      window.onload = function(){ 
         $("#feed").empty(); 

         /* if you want to execute it once at page load too:*/
         $("#feed").load("load.php"); 

         /* then load after $time milliseconds:*/
         setTimeout(function(){
             $("#feed").load("load.php");
         },'.$time.');
      }
      </script>';

Remember that setTimeout's offset is supposed to be in milliseconds.

请记住,setTimeout的偏移应该是以毫秒为单位。

Websockets are another solution to your problem if you have full server access (like to sockets and stuff). They involve basically creating a websocket connection when the script is first executed, and then server and client can send messages to each other as they please. These messages can include the data you wish.

如果您具有完整的服务器访问权限(如套接字和内容),Websockets是您的问题的另一种解决方案。它们涉及在首次执行脚本时基本上创建websocket连接,然后服务器和客户端可以随意发送消息。这些消息可以包含您希望的数据。