Javascript 将多个PHP文件的请求合并为一个请求

Javascript 将多个PHP文件的请求合并为一个请求,javascript,php,Javascript,Php,我有一个动态网络仪表板,显示温度、湿度、光线、噪音等数据。我有多个php文件,如temp.php、湿度.php、,light.php和noise.php负责从db中检索数据,然后我也有多个js文件,基本上使用setTimeout,每3秒将相应php文件中的数据显示到html页面 每个php文件如下所示,示例-hydrome.php: <?php session_start(); if(isset($_SESSION["user_id"])){ include

我有一个动态网络仪表板,显示温度、湿度、光线、噪音等数据。我有多个php文件,如temp.php、湿度.php、,light.php和noise.php负责从db中检索数据,然后我也有多个js文件,基本上使用setTimeout,每3秒将相应php文件中的数据显示到html页面

每个php文件如下所示,示例-hydrome.php:

    <?php
    session_start();
    if(isset($_SESSION["user_id"])){
    include('db.php');
    $unit = "820";
    $stmt = $db->prepare("SELECT hv FROM humidity where 
unitid=? order BY pk DESC LIMIT 1");
    $stmt->execute([$unit]);
    $humidity= $stmt->fetchColumn();
    $humidity=round($humidity, 2, PHP_ROUND_HALF_ODD);
    echo $humidity;
    $stmt->closeCursor();
    $db = null;
    }
    ?> 
该过程运行良好,但由于存在多个php请求,因此总体处理时间大约为2秒。我想将phps合并成一个php文件,将js文件合并成一个文件——因此只需要一个php请求即可检索所有数据


最好的方法是什么

希望下面的方法能对您有所帮助

在组合的php文件中:

<?php
  $humidity  = getHumidity(<parameter>);
  $temp = getTemp(<parameter>);
  $light = getLight(<parameter>);
  $retArr = array("humidity" => $humidity,"light" => $light, "temp" => $temp);
  echo json_encode($retArr);

  function getHumidity($param) { 
   // write your logic here to calculate the humidity
  }

  function getTemp($param) { 
   // write your logic here to calculate the temp
  }

  function getLight($param) { 
   // write your logic here to calculate the Light
  }

?>
在单个.js文件中:

jQuery(document).ready(function() {
    function foo() {
        jQuery.ajax({
            url : <path of your php file>,
            type : <Method GET/POST as per your requirement >,
            dataType : json,               
            async : false,
            success : function(data, status) {
               //update your html element with data
            },
    }

setInterval(foo, 3000);
});

调用3个仅读取数据库值的php文件需要2秒?我怀疑,合并这些文件将改善任何事情,因为问题似乎是其他的。。。
jQuery(document).ready(function() {
    function foo() {
        jQuery.ajax({
            url : <path of your php file>,
            type : <Method GET/POST as per your requirement >,
            dataType : json,               
            async : false,
            success : function(data, status) {
               //update your html element with data
            },
    }

setInterval(foo, 3000);
});