Ajax请求需要6秒才能完成,不知道为什么

时间:2010-08-24 13:33:13

标签: php javascript ajax zend-gdata

我正在开发一个用户界面,“仪表板”,其上有一些div框,其中包含与当前登录用户相关的信息。他们的日历,待办事项列表以及从谷歌电子表格中动态提取的一些统计数据。

我在这里找到: http://code.google.com/apis/spreadsheets/data/3.0/reference.html#CellFeed 可以使用这样的URL从工作表中请求特定单元格:
spreadsheets.google.com/feeds/cells/0AnhvV5acDaAvdDRvVmk1bi02WmJBeUtBak5xMmFTNEE/1/public/basic/R3C2

我简要介绍了Zend GData,但它似乎比我试图做的更复杂。

所以我写了两个php函数:(在hours.php中)
1.)根据参数行,列和表单执行生成的URL的file_get_contents() 2.)使用循环中的第一个来查找与给定名称相关联的列号。

所以基本上我使用jQuery做一个ajax请求,如下所示:

//开始js功能

function ajaxStats(fullname)
{
    $.ajax({
        url: "lib/dashboard.stats.php?name="+fullname,
        cache: false,
        success: function(html){
            document.getElementById("stats").innerHTML = html;
        }
    });
}

//结束js功能

//开始文件hours.php

<?php 
function getCol($name)
{
    $r=1;
    $c=2;
    while(getCell($r,$c,1) != $name)
    {    $c++;    }
    return $c;
}

function getCell($r, $c, $sheet)
{
    $baseurl = "http://spreadsheets.google.com/feeds/cells/";
    $spreadsheet = "0AnhvV5acDaAvdDRvVmk1bi02WmJBeUtBak5xMmFTNEE/";
    $sheetID = $sheet . "/";
    $vis = "public/";
    $proj = "basic/";
    $cell = "R".$r."C".$c;

    $url = $baseurl . $spreadsheet . $sheetID . $vis . $proj . $cell . "";
    $xml = file_get_contents($url);

    //Sometimes the data is not xml formatted,
    //so lets try to remove the url
    $urlLen = strlen($url);
    $xmlWOurl = substr($xml, $urlLen);

    //then find the Z (in the datestamp, assuming its always there)
    $posZ = strrpos($xmlWOurl, "Z");
    //then substr from z2end
    $data = substr($xmlWOurl, $posZ + 1);

    //if the result has more than ten characters then something went wrong
    //And most likely it is xml formatted
    if(strlen($data) > 10) 
    {
        //Asuming we have xml 
        $datapos = strrpos($xml,"<content type='text'>");
        $datapos += 21;
        $datawj = substr($xml, $datapos);
        $endcont = strpos($datawj,"</content>");
        return substr($datawj, 0,$endcont);
    }
    else
        return $data;
} 
?>

//结束hours.php

//开始dashboard.stats.php

<?php
session_start();
// This file is requested using ajax from the main dashboard because it takes so long to load,
// as to not slow down the usage of the rest of the page.

if (!empty($_GET['name']))
{
    include "hours.php";
    // GetCollumn of which C#R1 = users name
    $col = getCol($_GET['name']);
    // then get cell from each of the sheets for that user,
    // assuming they are in the same column of each sheet
    $s1 = getcell(3, $col, 1);
    $s2 = getcell(3, $col, 2);
    $s3 = getcell(3, $col, 3);
    $s4 = getcell(3, $col, 4);
    // Store my loot in the session varibles,
    // so next time I want this, I don't need to fetch it
    $_SESSION['fhrs'] = $s1;
    $_SESSION['fdol'] = $s2;
    $_SESSION['chrs'] = $s3;
    $_SESSION['bhrs'] = $s4;
}
//print_r($_SESSION);
?>
<!-- and finally output the information formated for the widget-->
<strong>You have:</strong><br/>
<ul style="padding-left: 10px;">
    <li>        <strong><?php echo $_SESSION['fhrs']; ?></strong> fundraising hours<br/></li>
    <li>earned $<strong><?php echo $_SESSION['fdol']; ?></strong> fundraising<br/></li>
    <li>        <strong><?php echo $_SESSION['chrs']; ?></strong> community service hours<br/></li>
    <li>        <strong><?php echo $_SESSION['bhrs']; ?></strong> build hours <br/></li>
</ul>

//结束dashboard.stats.php

我认为我失去4秒的地方是getCol() [hours.php]中的while循环 如何改善这一点,减少加载时间?

我应该废弃这个,然后去Zend GData吗? 如果是while循环,我是否应该尝试在用户数据库中的电子表格中存储每个用户列号,该用户数据库还验证登录?

1 个答案:

答案 0 :(得分:0)

我在while循环中没有正确的中断,即使在找到合适的人之后它仍然继续循环。

此外,请求需要一段时间才能转到Google电子表格。每个请求大约0.025秒。

我还与ZendGdata的用户进行了交谈,他们说请求速度不是很快。