简体   繁体   English

如何使用jquery ajax计算上传文件所需的时间

[英]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). 这样,然后计算开始值和结束值之间的差(如Taplar在评论中上面提到的那样)。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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