在所有其他 javascript 运行之后如何运行 jQuery 函数

发布于 2024-09-06 12:41:46 字数 790 浏览 9 评论 0原文

我有一个托管在 CMS(Squarespace)上的照片库页面,其中有一些自己的脚本可以异步加载缩略图。

然而,实际的大图像并未预加载,因此我决定将自己的脚本添加到混合中,以使浏览器将这些较大的图像加载到后台的缓存中,如下所示:

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

$(window).load(function(){
$.preLoadImages(
    "/picture/1.jpg",
    "/picture/2.jpg", //etc.
   );
});

我将代码放在 $(window). load() 因为这是一个后台脚本,它根本没有必要运行,它只是为了提高性能。

但是,我认为这个脚本在某种程度上阻止了 CMS 自己的缩略图预加载脚本。

我说得对吗?最重要的是,有没有办法规定我的脚本仅在页面上的所有其他脚本运行后运行?

欢呼

I have a photo gallery page hosted on a CMS (Squarespace) which has some of it's own scripts which load the thumbnails asynchronously.

The actual large images however are not preloaded, so I decided to add my own script into the mix to just make the browser load those larger images into the cache in the background, like this:

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

$(window).load(function(){
$.preLoadImages(
    "/picture/1.jpg",
    "/picture/2.jpg", //etc.
   );
});

I placed my code in a $(window).load() because this is a background script and it's not essential it even runs at all, it's just to improve performance.

However, I think this script is somehow blocking the CMS's own thumbnail preloading script.

Am I right? And most importantly, is there a way to dictate that my script only run after all other scripts on the page have run?

cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南烟 2024-09-13 12:41:46

JavaScript 始终在运行,例如,hover 事件不断触发,mousemove 等......脚本运行没有“结束”。

但是,就您而言,这不应阻止任何其他预加载...您也可以在此处使用 document.ready ,因为您实际上不需要在代码执行之前加载图像。

事实上,您实际上通过使用 window.load 来减慢页面速度...因为预加载稍后开始,此时它可以与浏览器之前的其他下载并行。而是使用 document.ready,如下所示:

$(function(){
  $.preLoadImages(
    "/picture/1.jpg",
    "/picture/2.jpg", //etc.
   );
});

JavaScript is always running, the hover event for example is firing constantly, mousemove, etc...there's no "end" to the script run.

However in your case, this shouldn't block any other preloading...also you can use document.ready here, since you don't actually need images loaded before your code executes.

In fact, you're actually slowing down the page by using window.load instead...since the preloading starts later, when it could be parallelized with other downloads earlier by the browser. Instead use document.ready, like this:

$(function(){
  $.preLoadImages(
    "/picture/1.jpg",
    "/picture/2.jpg", //etc.
   );
});
晒暮凉 2024-09-13 12:41:46

脚本从上到下加载,主体加载通常附加到现有加载 - 因此只要 $(function().. 位于页面末尾,它就会最后运行。(最后(根据尼克的评论)意味着文档的初始解析/运行)

Scripts are loaded top down, and body onloads are normally appended to existing onloads - so as long as that $(function().. is at the end of the page, it'll be ran last. (last (as per nick's comment) meaning the initial parse/run of the document)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文