How I can calculate AJAX response time? I need this in script, because I get back the server timestamp, but if the request take more than 1 second I need to add 1 second to the timestamp!
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You need to get the start time (just before the AJAX request is done), and then the end time when the script is complete. You can than work out the difference, and if it's greater than 60 seconds, do your thing.
//Before the AJAX function runs
var startTime = new Date().getTime();
//Place this code inside the success callback of your AJAX function
var endTime = new Date().getTime();
if ((endTime - startTime) > (60 * 1000)) {
//Took longer than 60 seconds
}
回答2:
You can set two timestamps, one before the AJAX call, and once it has completed, and then diff the two.
var currentTime = new Date();
Call the above code before and after your ajax call.
In order to get the datetime diff, see Reference: http://www.javascriptkit.com/javatutors/datedifference.shtml