0

I want to create a simple script help me for calculate the time is take when I upload a image to the server I have some thing like this

$(document).ready(function(){

    $("#but_upload").click(function(){

        var fd = new FormData();
        var files = $('#file')[0].files[0];
        fd.append('file',files);

        $.ajax({
            url: 'http://uploadtomyapi.com',
            type: 'post',
            data: fd,
            contentType: false,
            processData: false,
            success: function(response){
                // done my calculation here
        });
    });
});

I don't know this is the better way for do it but I am new in this, can some one help me thanks so mush.

  • 1
    You want to know how long the request took? If that's the case you can simply get a `Date.now()` before the ajax call and then again inside the success method. Subtract the two and you have the milliseconds that passed for the request. – Taplar Nov 12 '18 at 17:01
  • 1
    Possible duplicate of [Find out how long an Ajax request took to complete](https://stackoverflow.com/questions/3498503/find-out-how-long-an-ajax-request-took-to-complete) – Heretic Monkey Nov 12 '18 at 17:02

1 Answers1

0

Something like this, and then calculate the difference between the start and end value (like mentioned by Taplar above in the comments).

<script>
var startTime, EndTime;
$(document).ready(function () {
    $("#but_upload").click(function () {
        var fd = new FormData();
        var files = $('#file')[0].files[0];
        fd.append('file', files);
        $.ajax({
            url: 'http://uploadtomyapi.com',
            type: 'post',
            data: fd,
            contentType: false,
            processData: false,
            beforeSend: function () {
                startTime = Date.now();
            },
            success: function (response) {
                // done my calculation here
            },
            complete: function () {
                endTime = Date.now();
            }
        });
    });
});
</script>
netfed
  • 602
  • 8
  • 18