I'm trying to time a ajax request, but I get random elapsed time:
startTime = new Date();
$.ajax({
...
success: function(response){
endTime = new Date();
timeDiff = endTime - startTime;
elapsed = Math.round(timeDiff % 60);
alert(elapsed + ' seconds elapsed');
}
}
...Like after 2 seconds I get 36 seconds, or 59 seconds, or 1 second etc..
What am I doing wrong here?
Use / instead of %
% is modulo operator.
Proper code to calculate number of seconds between to dates:
(endTime.getTime() - startTime.getTime()) / 1000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With