Find out how long an Ajax request took to complete(找出完成一个 Ajax 请求需要多长时间)
问题描述
找出特定 $.ajax()
请求所用时间的好方法是什么?
What is a good way to find out how long a particular $.ajax()
request took?
我想获取此信息,然后将其显示在页面上的某处.
I would like to get this information and then display it on the page somewhere.
ANSWER??::::
我是 javascript 新手,如果您不想内联成功"函数(因为它将是一个更大的函数),这是我能想到的最好的方法.做这个?我觉得我把事情复杂化了……:
I'm new to javascript, this is the best that I could come up with if you don't want to inline the "success" function (because it will be a much bigger function) Is this even a good way to do this? I feel like I'm over complicating things...:
makeRequest = function(){
// Set start time
var start_time = new Date().getTime();
$.ajax({
async : true,
success : getRquestSuccessFunction(start_time),
});
}
getRquestSuccessFunction = function(start_time){
return function(data, textStatus, request){
var request_time = new Date().getTime() - start_time;
}
}
推荐答案
@codemeit 是对的.他的解决方案如下所示,使用 jQuery 处理 ajax 请求.这会以毫秒为单位返回请求时间.
@codemeit is right. His solution looks something like the following using jQuery for the ajax request. This returns the request time in milliseconds.
var start_time = new Date().getTime();
jQuery.get('your-url', data, function(data, status, xhr) {
var request_time = new Date().getTime() - start_time;
});
这篇关于找出完成一个 Ajax 请求需要多长时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:找出完成一个 Ajax 请求需要多长时间


基础教程推荐
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01