简体   繁体   中英

How Calculate the time is take on upload file using jquery ajax

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM