简体   繁体   English

如何从Ajax中的任何Api提取数据时计算提取时间

[英]How to calculate Fetching Time while fetching data from any Api in Ajax

  1. I just want to show the amount of data fetched from Api. 我只想显示从Api提取的数据量。

  2. output should be like 10% fetched 90 % remaining like that. 输出应该像10%一样,剩下90%那样。

  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 . 我一直在研究basecamp api,这将浪费大量时间来获取任务,活动等。因此,我只想显示在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. 我已经在Google上搜索并找到了一个链接: Monitoring_progress,但是如果有任何机构可以向我建议从哪里开始,我将不胜感激。

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. 大部分时间都花在basecamp服务器上,而不是在请求中。 The link you posted offers a way for basecamp to push progress messages. 您发布的链接为basecamp提供了一种发送进度消息的方式。 If they don't do so, you will get nothing. 如果他们不这样做,您将一无所获。

You can imagine the basecamp servers as a wall. 您可以将basecamp服务器想象成一堵墙。 Behind the wall is the progress, but you can't see behind a wall and you are not allowed by basecamp to climb it. 墙的后面是进度,但您看不到墙的后面, basecamp也不允许您攀登它。

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 . 比您计算对url的所有请求的mean time That time will be good enough. 那个时间将足够好。

I recommend something like this: 我推荐这样的东西:

  • Measure times by yourself for every request, 10 times. 自己量度每个请求的时间10次。
  • Calculate the mean of the times ( sum them and divide by 10 (the number of requests)) 计算时间的平均值(相加并除以10(请求数))
  • 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. secondsPerPercent = timeInSeconds / 100 <-这是要填充百分比所需要经过的秒数。
  • make a javascript timer (code for it can be found on google) that updates the percent every secondsPerPercent . 制作一个JavaScript计时器(可以在google上找到其代码),该计时器secondsPerPercent更新一次百分比secondsPerPercent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM