使用setInterval定时器通过AJAX调用php文件 - 初始加载时间过长

时间:2016-03-22 18:22:14

标签: javascript php jquery ajax

我使用以下函数每10秒调用一次php文件:

$.ajaxSetup ({
    cache: false
});    

$(document).ready(function() {
        setInterval(function() {
            $.get("http://192.168.3.30/statustablephp.php", function(systemsupdown) {
                $('#systemsud').html(systemsupdown);
            });
        }, 10000);
    });

php文件是一个从服务器ping大约10个IP并报告可达状态和平均响应时间的表。

一旦加载,该功能就能正常工作,但初始加载需要1-2分钟(有时更长!),在此期间窗口是空白的。该表在初始加载后每10秒成功更新一次,因此它似乎不是文件响应时间的问题。有没有办法减少初始加载时间?我无法理解为什么在10秒后文件没有加载任何问题时需要这么长时间。·

编辑:添加被调用的php:

<?php

$myServices = array
    (
    array(
        "name" => "abaqis", 
        "ip" => "www.abaqis.com", //[0]
    ),
    array(
        "name" => "SNFs Admissions",
        "ip" => "www.helpmeenroll.com", //[1]
    ),
    array(
        "name" => "INTERACT",
        "ip" => "interact2.net",//[2]
    ),
    array(
        "name" => "IT Help Desk",
        "ip" => "help.provideris.com", //[3] 
    ),
    array(
        "name" => "ODH Gateway",
        "ip" => "interact2.net", //Not the real IP; picked one that pings fast so the function doesn't get hung up on this dead one; want to reserve the spot for later though so all the other numbers don't get messed up; taking the place of [4]
    ),
    array(
        "name" => "Omnicare Pharmacy",
        "ip" => "interact2.net", //Not the real IP; picked one that pings fast so the function doesn't get hung up on this dead one; want to reserve the spot for later though so all the other numbers don't get messed up; taking the place of [5]
    ),
    array(
        "name" => "OptimaSolutions",
        "ip" => "www.foundationssolutions.com", //[6]
    ),
    array(
        "name" => "Outlook Webmail",
        "ip" => "mail.fhs-is.com", //[7]
    ),
    array(
        "name" => "PointClickCare",
        "ip" => "www4.pointclickcare.com", //[8]
    ),
    array(
        "name" => "Procurement Partners",
        "ip" => "buyer.procurementpartners.com", //[9]
    ),
    array(
        "name" => "Quadax",
        "ip" => "interact2.net", //Not the real IP; picked one that pings fast so the function doesn't get hung up on this dead one; want to reserve the spot for later though so all the other numbers don't get messed up; taking the place of [10]
    ),
    array(
        "name" => "Remedi Pharmacy",
        "ip" => "www.remedirx.com", //[11]
    ),
    array(
        "name" => "Sex Offender Registry",
        "ip" => "www.icrimewatch.net", //[12]
    ),
    array(
        "name" => "SigmaCare",
        "ip" => "login.sigmacare.com", //[13]
    ),
    array(
        "name" => "UltiPro",
        "ip" => "interact2.net", //Not the real IP; picked one that pings fast so the function doesn't get hung up on this dead one; want to reserve the spot for later though so all the other numbers don't get messed up; taking the places of [14]
    ),
    array(
        "name" => "UR Patient Management",
        "ip" => "provider-ur.net", //[15]
    ),
);

function ping($target){

$result = array();

$cmd_result = shell_exec("ping -n 3 -w 500 ". $target);

$result = explode(",",$cmd_result);

if(eregi("Received = 0", $result[1])){
return false;
}
else{
    return true;
    }
};

$output = array();
$output2 = array();
$output3 = array();

function pinglat($tar) {
exec("ping -n 2 -w 500 $tar", $output, $status);
$output2 = explode(",",$output[8]);
$output3 = explode("=",$output2[2]);
return $output3[1];
};

$latencyResults = array();

foreach ($myServices as $value) {
    $pinglatResult = pinglat($value["ip"]);
    array_push($latencyResults, $pinglatResult);

};

$serverResults = array();
$ti = time();

foreach ($myServices as $value) {
       $pingResult = ping($value["ip"]);
       array_push($serverResults, $pingResult);                
};

    ?>


php文件中唯一的附加信息是html / css表本身。没有图像正在加载。整个php文件是自包含的 - 我正在为公司使用内部网软件,我唯一真正的选择是使用只需要某种类型和一定数量的代码的小部件。所以我只使用JS / Ajax部件的小部件,并将其他所有内容都放在php文件中。

当它加载时,它看起来像这样(并且每10秒更新一次):
Status Table

0 个答案:

没有答案