gpt4 book ai didi

php - xmlhttprequest 在函数中不能正常工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:09:40 27 4
gpt4 key购买 nike

有人知道为什么 upload.onprogress 在单独的函数上不能正常工作吗?

代码正常运行(进度条缓慢移动):

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

但是如果我把它放到函数中,它就不再起作用了:

xhr.upload.onprogress = uploadProgress(event);

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

在第二个代码中,进度条在文件上传完成后直接跳到100%,而不是在上传过程中很好地跳到100%


所以,我已经尝试了提供的解决方案,如果我将函数放入其中,它确实有效。有没有办法放到函数外面?

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

最佳答案

使用 uploadProgress(event); 调用函数本身并将返回值分配给 xhr.upload.onprogress 而不是将其分配为回调函数:

xhr.upload.onprogress = uploadProgress;

关于php - xmlhttprequest 在函数中不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9707839/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com