javascript - Check response time of a website with ajax - Stack Overflow

Hi I want to check response times of website. Here is my code, I got some values but doesn't show

Hi I want to check response times of website. Here is my code, I got some values but doesn't show reality. What is the problem with these codes. Is it sth related with cache?? Furthermore how to show if page doesn't exit or unavailable.

<script type="text/javascript" src="jquery.min.js"></script> <script type="text/javascript">
 var start = new Date();
 $.ajax ({
 url: '',
 plete : function()
 {
  alert(new Date() - start)
 },
        }); 
</script>

Hi I want to check response times of website. Here is my code, I got some values but doesn't show reality. What is the problem with these codes. Is it sth related with cache?? Furthermore how to show if page doesn't exit or unavailable.

<script type="text/javascript" src="jquery.min.js"></script> <script type="text/javascript">
 var start = new Date();
 $.ajax ({
 url: 'http://www.example.',
 plete : function()
 {
  alert(new Date() - start)
 },
        }); 
</script>
Share Improve this question asked Dec 10, 2012 at 12:22 user1874941user1874941 3,1734 gold badges22 silver badges31 bronze badges 1
  • 1 "Furthermore how to show if page doesn't exit or unavailable." Ask one question per question. – T.J. Crowder Commented Dec 10, 2012 at 12:24
Add a ment  | 

3 Answers 3

Reset to default 5

The code itself is fine assuming that the code is running on the same origin as the one it's checking; you can't use ajax cross-origin unless both ends (client and server) support and are using CORS.

It could be caching, yes, you'd have to refer to the browser tools (any decent browser has a Network tab or similar in its developer tools) to know for sure. You can also disable caching by setting cache: false in the ajax call (see the ajax documentation for details), although that's a somewhat synthetic way to do it. A better way would be to ensure that whatever URL you're using for this timing responds with cache headers telling the browser (and proxies) not to cache it.

You can tell if the page doesn't exist or is "unavailable" (whatever that means) by hooking the error function and looking at the information it gives you:

var start = new Date();
$.ajax ({
    url: 'http://www.example.',
    error : function(jqxhr, status, ex) {
        // Look at status here
    },
    plete : function()
    {
        alert(new Date() - start)
    },
}); 

The arguments given to error are also described in the docs linked above.

You can't do this due to the same origin policy.

One trick would be to create an image and measure the time until the onerror event fires.

var start = new Date();

var img = new Image();

img.onerror = function() {
    alert(new Date() - start);
};

img.src = 'http://www.example./';

Append a random number to the url to prevent caching.

I'd use browser extensions such as Firebug or Chrome Developer Tools to measure Ajax response times.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745441313a4627845.html

相关推荐

  • javascript - Check response time of a website with ajax - Stack Overflow

    Hi I want to check response times of website. Here is my code, I got some values but doesn't show

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信