简体   繁体   English

非阻塞Javascript

[英]Non-blocking Javascript

I am wondering if it is possible to load javascript in a way in which it does not block the user experience. 我想知道是否有可能以不妨碍用户体验的方式加载javascript。 I am not sure how to achieve the same, but I am looking for a cross-browser solution. 我不知道如何实现相同,但我正在寻找一个跨浏览器的解决方案。 I am wondering if someone can guide me in the right direction. 我想知道是否有人可以指导我朝着正确的方向前进。 Placing the js at the bottom of the page does not work too well. 将js放在页面底部不能很好地工作。

Thank you for your time. 感谢您的时间。

Javascript runs in a single-thread, so if you have massive Javascript calls, let say with librairies like ExtJS, it's normal that it can be slow. Javascript在单线程中运行,所以如果你有大量的Javascript调用,那么就像ExtJS这样的librairies一样,它可能很慢。 You might however consider the following alternatives: 但是,您可以考虑以下替代方案:

First and foremost, try to optimize the code as much as you can. 首先,尽可能多地优化代码。

Then, you could use timers in Javascript to simulate asynchronous work. 然后,您可以在Javascript中使用计时器来模拟异步工作。 Here is a good example on how to do this : http://ejohn.org/blog/how-javascript-timers-work/ 以下是如何执行此操作的一个很好的示例: http//ejohn.org/blog/how-javascript-timers-work/

If you want more information, here are some additional tips on how to attempt to reduce Javascript freezing time. 如果您想了解更多信息,请参阅以下有关如何尝试减少Javascript冻结时间的一些其他提示。

http://debuggable.com/posts/run-intense-js-without-freezing-the-browser:480f4dd6-f864-4f72-ae16-41cccbdd56cb http://debuggable.com/posts/run-intense-js-without-freezing-the-browser:480f4dd6-f864-4f72-ae16-41cccbdd56cb

Good luck ! 祝好运 !

Quoting this answer : 引用这个答案

Javascript resource requests are indeed blocking, but there are ways around this (to wit: DOM injected script tags in the head, and AJAX requests) which without seeing the page myself is likely to be what's happening here. Javascript资源请求确实是阻塞的,但是有很多方法可以解决这个问题(例如:头脑中注入DOM的脚本标记,以及AJAX请求),而我自己看不到页面可能就是这里发生的事情。

Including multiple copies of the same JS resource is extremely bad but not necessarily fatal, and is typical of larger sites which might have been accreted from the work of separate teams, or just plain old bad coding, planning, or maintenance. 包括相同JS资源的多个副本是非常糟糕的,但不一定是致命的,并且是典型的较大的站点,可能已经从单独的团队的工作中累积,或者只是简单的旧编码,计划或维护。

As far as yahoo's recommendation to place scripts at the bottom of the body, this improves percieved response times, and can improve actual loading times to a degree (because all the previous resources are allowed to async first), but it will never be as effective as non-blocking requests (though they come with a high barrier of technical capability). 至于雅虎建议将脚本放在正文的底部,这可以提高执行的响应时间,并且可以在一定程度上改善实际加载时间(因为所有以前的资源都允许首先异步),但它永远不会那么有效作为非阻塞请求(尽管它们具有很高的技术能力)。

You can take a look at a YUI blog entry about Non-Blocking Javascript . 您可以查看有关非阻止Javascript的YUI博客条目。

I believe you can use Workers, but it seems to be implemented in FF3.5, but few other ones. 我相信你可以使用Workers,但它似乎是在FF3.5中实现的,但很少有其他的。

See http://hacks.mozilla.org/2009/07/working-smarter-not-harder/ http://hacks.mozilla.org/2009/07/working-smarter-not-harder/

When a page is loading it can only download 2 javascript files in parallel at any one time so trying to keeping the number of javascript files down and their size down (with Minification,obsfucation and GZipping) will help with the loading experience. 当页面加载时,它一次只能并行下载2个javascript文件,因此试图保持javascript文件的数量减少并缩小它们的大小(使用Minification,obsfucation和GZipping)将有助于加载体验。

Using callbacks in your javascript will also help with items being non-blocking when javascript is running. 在javascript运行时,在javascript中使用回调也有助于项目无阻塞。

An example in jQuery would be jQuery中的一个例子是

$('#id').click(function(){
  $.post('url',data,function(callbackdata){//do something
         });

});

setTimeout with a small delay will allow your control flow to proceed while scheduling another function to execute later. 具有较小延迟的setTimeout将允许您的控制流继续进行,同时安排另一个函数以便稍后执行。 This is especially useful to prevent the UI from blocking or being inadvertently dependent on the successful execution of the other function. 这对于防止UI阻塞或无意中依赖于其他功能的成功执行特别有用。

I find it very useful to prevent javascript errors from interfering with bound events. 我发现防止javascript错误干扰绑定事件非常有用。 For example, to install a submit handler on a form: 例如,要在表单上安装提交处理程序:

$('#form').submit(function() {
  setTimeout(function() {
    // Submit handler function, i.e. do an ajax submission of the form
    $.ajax(...etc...);
  }, 1);
  // Return false before the handler executes, ensuring the form won't manually submit
  // in the event of a js error in the handler
  return false;
});

如果您有一些对于立即加载并不重要的JavaScript,则推迟执行JavaScript可能是一个非常好的解决方案。

Have a look at this jQuery plugin (http://code.google.com/p/funky-jq-plugins/wiki/nonblocking). 看看这个jQuery插件 (http://code.google.com/p/funky-jq-plugins/wiki/nonblocking)。

It aims at using timers to emulate a multi-threaded environment where the UI thread is not frozen by demanding operations like iteration over long lists. 它旨在使用计时器来模拟多线程环境,在这种环境中,UI线程不会被需要的操作(例如长列表上的迭代)冻结。 Very cool stuff ... I wrote it :) 很酷的东西......我写的:)

Ciao for now Ciao现在

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

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