简体   繁体   English

在后台线程上将HTML加载到Div中?

[英]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. 您在上面发布的代码看起来是正确的,不应该导致您描述的内容。

将 HTML 装入<div></div><div id="text_translate"><p><em><strong>小提琴</strong>: <a href="https://jsfiddle.net/x9ghz1ko/" rel="nofollow noreferrer">https://jsfiddle.net/x9ghz1ko/</a> (带评论。)</em></p><p> 我有一个<strong>HTML 文档</strong>,在这个文档里面,有<strong>两个部分</strong>: &lt;section class="desktop_only"&gt;和&lt;section class="mobile_only"&gt; - 在代码中,在这些部分之前,有一个<strong>脚本</strong>,这个脚本应该<strong>删除两个部分之一</strong>,但问题是:为此,我必须将脚本放在部分之后,但我不能这样做,因为部分内有更多脚本,我必须删除它们在他们跑之前。</p><p> 我的代码的简化版本:(……不工作。) </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script&gt; device = "desktop"; // This could be either: "desktop", or: "mobile" if(device === "desktop"){ document.querySelector(".mobile_only").remove(); // Not doing anything. } else { document.querySelector(".desktop_only").remove(); // Not doing anything. } &lt;/script&gt; &lt;section class="desktop_only"&gt; &lt;script src="example.js"&gt;&lt;/script&gt; &lt;script&gt; console.log("desktop_only") &lt;/script&gt; &lt;div&gt; desktop &lt;/div&gt; &lt;/section&gt; &lt;section class="mobile_only"&gt; &lt;script src="example.js"&gt;&lt;/script&gt; &lt;script&gt; console.log("mobile_only") &lt;/script&gt; &lt;div&gt; mobile (Delete this.) &lt;/div&gt; &lt;/section&gt;</pre></div></div><p></p><p> <strong>也许一种解决方案是:</strong>我有一个空的&lt;div&gt; ,称为".platform_dependent" ,我有两个外部文件,称为: desktop_only.something和mobile_only.something然后我将这些文件的内容加载到: platform_dependent.innerHTML ...但我不知道该怎么做?</p></div> - load HTML into <div>

暂无
暂无

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

相关问题 HTML DIV作为另一个DIV的背景 - HTML DIV as the Background of another DIV 加载html或div,菜单 - load html or div, menu 将 HTML 装入<div></div><div id="text_translate"><p><em><strong>小提琴</strong>: <a href="https://jsfiddle.net/x9ghz1ko/" rel="nofollow noreferrer">https://jsfiddle.net/x9ghz1ko/</a> (带评论。)</em></p><p> 我有一个<strong>HTML 文档</strong>,在这个文档里面,有<strong>两个部分</strong>: &lt;section class="desktop_only"&gt;和&lt;section class="mobile_only"&gt; - 在代码中,在这些部分之前,有一个<strong>脚本</strong>,这个脚本应该<strong>删除两个部分之一</strong>,但问题是:为此,我必须将脚本放在部分之后,但我不能这样做,因为部分内有更多脚本,我必须删除它们在他们跑之前。</p><p> 我的代码的简化版本:(……不工作。) </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script&gt; device = "desktop"; // This could be either: "desktop", or: "mobile" if(device === "desktop"){ document.querySelector(".mobile_only").remove(); // Not doing anything. } else { document.querySelector(".desktop_only").remove(); // Not doing anything. } &lt;/script&gt; &lt;section class="desktop_only"&gt; &lt;script src="example.js"&gt;&lt;/script&gt; &lt;script&gt; console.log("desktop_only") &lt;/script&gt; &lt;div&gt; desktop &lt;/div&gt; &lt;/section&gt; &lt;section class="mobile_only"&gt; &lt;script src="example.js"&gt;&lt;/script&gt; &lt;script&gt; console.log("mobile_only") &lt;/script&gt; &lt;div&gt; mobile (Delete this.) &lt;/div&gt; &lt;/section&gt;</pre></div></div><p></p><p> <strong>也许一种解决方案是:</strong>我有一个空的&lt;div&gt; ,称为".platform_dependent" ,我有两个外部文件,称为: desktop_only.something和mobile_only.something然后我将这些文件的内容加载到: platform_dependent.innerHTML ...但我不知道该怎么做?</p></div> - load HTML into <div> 在div中加载html,然后将其删除 - Load html in div and then remove it 将html链接加载到div中 - load html link into div 在页面加载上设置div的背景 - setting background of div on page load 在页面加载时偏移Div背景 - Offset Div background on page load HTML:具有固定背景的可滚动div - Html: scrollable div with fixed background 如何在 html 中使用 div 作为背景 - How to use div for background in html 如何在html中后台加载图像 - how to load an image in background in html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM