0

I want to know how long it takes for my AJAX call to drop on server and get response. I made this script to test it but it gives me strange results.

This is my client-side

$('.go').click(function(){
   var dateStart = new Date().getTime();
   $.post("timestamp_get.php", {}, function(data){
      var dateEnd = new Date().getTime();
      alert(dateStart + '\n' + data + '\n' + dateEnd);
   });
});

This is my server-side (timestamp_get.php)

<?php
$var = microtime(true);
$var = str_replace('.', '', $var);
echo $var;

Response:

1331718943881 - Right after I click on button
1331718943889 - Right after I get response from server
13317187704121 - PHP Response

As you can see it differs very much, why it is that and how can I fix that.

And also, how can I output milliseconds instead of UNIX time stamp?

Stan
  • 25,744
  • 53
  • 164
  • 242

2 Answers2

0

I just trimmed the last number.

Stan
  • 25,744
  • 53
  • 164
  • 242
0

You need to compare seconds, but you compare seconds with miliseconds

Replace this

$var = microtime(true);
$var = str_replace('.', '', $var);
echo $var;

with this

echo date('U');
Narek
  • 3,813
  • 4
  • 42
  • 58