简体   繁体   中英

xmlhttprequest does not work properly in function

is anyone know why upload.onprogress does not work correctly if it's on separate function?

Code work properly (the progress bar slowly moving):

        xhr.upload.onprogress = function(e) {
            if (e.lengthComputable) {
                progress.value = (e.loaded / e.total) * 100;
            }
        };  

but if I put it into function, it does not work anymore:

xhr.upload.onprogress = uploadProgress(event);

function uploadProgress(e) {
    if (e.lengthComputable) {
        progress.value = (e.loaded / e.total) * 100;
    }
}   

On the second code, the progress bar directly jump into 100% after file finished uploaded instead of, move nicely into 100% during the upload


so, I've tried the solution provided, it actually work, if I put the function inside. Is there no way to put it outside the function?

        function uploadFile(blobFile, fileName) {
            ...
            ...

            // Listen to the upload progress for each upload.
            xhr.upload.onprogress = uploadProgress;

            // Progress Bar Calculation why it has to be in uploadFile function..
            function uploadProgress(e) {
                if (e.lengthComputable) {
                    progress.value = (e.loaded / e.total) * 100;
                }
            }                            

            uploaders.push(xhr);
            xhr.send(fd);
        }  

        //it does not work if I put it outside the function. is there anyway to do this?  
        function uploadProgress(e) {
             if (e.lengthComputable) {
                 progress.value = (e.loaded / e.total) * 100;
             }
        }   

With uploadProgress(event); you call the function itself and assign the return value to xhr.upload.onprogress instead of assigning it as a callback function:

xhr.upload.onprogress = uploadProgress;

In the second example you should use

xhr.upload.onprogress = uploadProgress;

not

xhr.upload.onprogress = uploadProgress(event);

You've assigned the result of calling the function, rather than a reference to the function.

How about defining the function before assigning as callback? JavasCript sometimes has problems when defining functions later.

I mean you can replace:

// Listen to the upload progress for each upload.
xhr.upload.onprogress = uploadProgress;

// Progress Bar Calculation
function uploadProgress(e) {
    if (e.lengthComputable) {
         progress.value = (e.loaded / e.total) * 100;
    }
}

with

// Progress Bar Calculation
function uploadProgress(e) {
    if (e.lengthComputable) {
         progress.value = (e.loaded / e.total) * 100;
    }
}
// Listen to the upload progress for each upload.
xhr.upload.onprogress = uploadProgress;

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