問題描述
找出特定 $.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 請求需要多長時間的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!