[英]Load HTML into Div on background thread?
I'm loading an external HTML file into a DIV using the following method: 我正在使用以下方法将外部HTML文件加载到DIV中:
var docName = name + ".html";
$("#displayPanel").load(docName);
then I call getScript on the javascript file controlling that HTML file, like so: 然后我在控制该HTML文件的javascript文件上调用getScript,如下所示:
docName = name + ".js";
$.getScript(docName, function( data, textStatus, jqxhr ) {
if (textStatus==="success") {
lightItUp(); //load method
}
});
It loads correctly. 它正确加载。
The issue is that with large HTML/JS files the DOM is frozen and user interaction is disabled until the load has completed. 问题是,对于大型HTML / JS文件,DOM被冻结,用户交互被禁用,直到加载完成。 Is it possible to put the div loading on a 'background thread' and receive a completion notification? 是否可以将div加载到“后台线程”并接收完成通知?
Not sure if I understood your question correctly. 不确定我是否正确理解了你的问题。
If you want to make it as if the div is loading on a background thread and receive a notif upon completion, you can have the div display:none
at first and make it display:block
in the callback. 如果你想让它好像div在后台线程上加载并在完成时收到一个notif,你可以让div display:none
并且使它display:block
回调中的display:block
。
$( "#aDivInDisplayPanel" ).load( docName, function() {
//load completed.
$(this).fadeIn();
});
Have you considered using HTML5 Web Workers? 您是否考虑过使用HTML5 Web Workers?
http://www.w3schools.com/html/html5_webworkers.asp http://www.w3schools.com/html/html5_webworkers.asp
When executing scripts in an HTML page, the page becomes unresponsive until the script is finished. 在HTML页面中执行脚本时,页面将变为无响应,直到脚本完成。
A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting the performance of the page. Web worker是一种在后台运行的JavaScript,与其他脚本无关,不会影响页面的性能。 You can continue to do whatever you want: clicking, selecting things, etc., while the web worker runs in the background. 您可以继续执行任何操作:在Web工作程序在后台运行时,单击,选择内容等。
Unless you make AJAX synchronous somehow, the code above can't block the UI unless the browser needs a long time to process the HTML (IE 6.6 could take 1-2 minutes to parse HTML loaded this way) or you have something in lightItUp()
that takes a long time. 除非你以某种方式使AJAX同步,否则上面的代码不能阻止UI,除非浏览器需要很长时间来处理HTML(IE 6.6可能需要1-2分钟来解析以这种方式加载的HTML)或者你在lightItUp()
有一些东西lightItUp()
这需要很长时间。
The next step is to fire up your browser's performance tool and check where the time goes. 下一步是启动浏览器的性能工具并检查时间。 The code that you posted above looks correct and should not cause what you describe. 您在上面发布的代码看起来是正确的,不应该导致您描述的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.