AJAX response time

谁说胖子不能爱 提交于 2020-01-24 12:11:34

问题


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



来源:https://stackoverflow.com/questions/5728833/ajax-response-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!