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?

1

2 Answers 2

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');
0

I just trimmed the last number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.