简体   繁体   English

Web 浏览器(Chromium/Firefox)在文件对话框后 1-2 秒无响应

[英]Web Browser (Chromium/Firefox) becomes unresponsive for 1-2sec after file dialog

How can I improve this code in order to remove unresponsivness / page lag after selecing a file from file dialog and clicking OK?从文件对话框中选择文件并单击确定后,如何改进此代码以消除不响应/页面滞后?

I've been testing files with sizes around 50-100 KB我一直在测试大小约为 50-100 KB 的文件

 function handleFileSelect(evt) { var files = evt.target.files; // FileList object // files is a FileList of File objects. List some properties. var output = []; for (var i = 0, f; f = files[i]; i++) { output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ', f.size, ' bytes, last modified: ', f.lastModifiedDate? f.lastModifiedDate.toLocaleDateString(): 'n/a', '</li>'); } document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>'; } document.getElementById('files').addEventListener('change', handleFileSelect, false);
 <input type="file" id="files" name="files[]" multiple /> <output id="list"></output>

I'm running this page on localhost and I'm using SSD我在 localhost 上运行这个页面,我正在使用 SSD

Thanks谢谢

Your code is perfectly fine.你的代码非常好。 Try measuring the performance to investigate further:尝试测量性能以进一步调查:


在此处输入图像描述

Use Promises in your handleFileSelect function or make async function of it.在您的handleFileSelect function 中使用 Promises 或使其异步function 。

Your code works and there is nothing wrong with it.You can only improve performance by first measuring it and then taking appropriate actions.您的代码可以正常工作并且没有任何问题。您只能通过首先对其进行测量然后采取适当的措施来提高性能。

For eg You may refactor the code to a cleaner approach -例如,您可以将代码重构为更简洁的方法 -

 let handleFileSelect = (evt) => { let files = evt.target.files; // FileList object let output = [...files].map((file) => { return `<li> <strong>${escape(file.name)}</strong> (${file.type || "n/a"}) - ${file.size} bytes, last modified: ${ file.lastModifiedDate? file.lastModifiedDate.toLocaleDateString(): "n/a" } </li>`; }); document.getElementById("list").innerHTML = `<ul>${output.join("")}</ul>`; }; document.getElementById("files").addEventListener("change", handleFileSelect, false);
 <input type="file" id="files" name="files[]" multiple /> <output id="list"></output>

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

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