dongnai8013 2014-04-22 10:28
浏览 163
已采纳

如何在Ajax中从任何Api获取数据时计算获取时间

  1. I just want to show the amount of data fetched from Api.

  2. output should be like 10% fetched 90 % remaining like that.

  3. I'm searching how should I calculate the amount the amount retreived and remaning data left to be fetched.

  4. I have been working on basecamp api It would waste lots of time to fetch tasks , activities etc. So just want that I want to show time left while fetching in Ajax .

I have googled and found a link: Monitoring_progress but if any body could suggest to me where to start I would much appreciate.

  • 写回答

1条回答 默认 最新

  • dongzongpeng6474 2014-04-22 11:16
    关注

    As far as I know there is no way to get the exact time, but you can get an approximation.

    Why you can't have an exact time ?

    Most of the time is spent in the basecamp servers, not in the request. The link you posted offers a way for basecamp to push progress messages. If they don't do so, you will get nothing.

    You can imagine the basecamp servers as a wall. Behind the wall is the progress, but you can't see behind a wall and you are not allowed by basecamp to climb it.

    What to do ?

    You can approximate this time. With a method similar to what is posted here:

    var ajaxTime= new Date().getTime();
    $.ajax({
        type: "POST",
        url: "some.php",
    }).done(function () {
        var totalTime = new Date().getTime()-ajaxTime;
    });
    

    you can measure how long it will take to complete a request.

    Than you calculate the mean time of all requests made to an url. That time will be good enough.

    I recommend something like this:

    • Measure times by yourself for every request, 10 times.
    • Calculate the mean of the times ( sum them and divide by 10 (the number of requests))
    • Save the result and send it when you initiate your application and store in the browser.
    • When user makes request, get the precalculated time and display a timer.

    Update

    To display a fix amount of time as a percent you can do like this:

    • secondsPerPercent = timeInSeconds / 100 <-- this is how many seconds need to pass for a percent to be filled.
    • make a javascript timer (code for it can be found on google) that updates the percent every secondsPerPercent.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 nslt的可用模型,或者其他可以进行推理的现有模型
  • ¥15 arduino上连sim900a实现连接mqtt服务器
  • ¥15 vncviewer7.0安装后如何正确注册License许可证,激活使用
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并2
  • ¥66 关于人体营养与饮食规划的线性规划模型
  • ¥15 基于深度学习的快递面单识别系统
  • ¥15 Multisim仿真设计地铁到站提醒电路
  • ¥15 怎么用一个500W电源给5台60W的电脑供电
  • ¥15 请推荐一个轻量级规则引擎,配合流程引擎使用,规则引擎负责判断出符合规则的流程引擎模板id
  • ¥15 Excel表只有年月怎么计算年龄