循环中的 Clone AppendTo 函数会重载对服务器的请求 - 应该从缓存加载! (Jquery)

发布于 2024-12-08 20:33:32 字数 905 浏览 3 评论 0原文

此函数克隆图像列表的声音对象。

问题在于,当对象写入 DOM 中时,浏览器查询服务器将其作为 200 请求加载。然而,它只是一遍又一遍地加载 4 个不同的声音文件。因此它应该从浏览器的缓存中作为 304 请求加载。

所有声音文件最初都以 HTML 形式加载,然后进行克隆。每当刷新页面时,HTML 中的声音文件都会收到 304 请求并从缓存加载,但克隆的任何文件都会加载 200 请求(不是从缓存)!

无论如何,我可以在浏览器不向服务器发送请求以加载到 DOM 的情况下克隆这些对象吗?完全在客户端吗?数据已经在 HTML 中写入一次。

我不想向服务器发送任何请求来克隆这些对象。由于它克隆了 50 多个,因此每秒有 50 个请求发送到服务器。超载了。

var increment = 0;
$("img").each(function(i) { //loop for each item
    if (increment > 4){
        increment = 0;  
    }

    if (i != 0) {  //only clone if there are more than one 'img' items      
        $("#hover-" + increment)  //item to clone     
          .clone(true)
          .attr("id", "beep-" + i)  //new name of object
          .appendTo($(this).parent()); 
    }

    $(this).data("instance", i);  //save the integer for loop
    increment=Math.floor(Math.random()*3);  //change increment
})

This function clones sound objects for a list of images.

The problem is that when the object is written in the DOM, the browser querys the server loads it as a 200 request. However, it is only loading 4 different sound files, over and over again. So it should load as a 304 request from the browser's cache.

All of the sound files are initially loaded in HTML and then cloned. Whenever the page is refreshed the sound files in the HTML receive 304 requests and are loaded from cache, but any file that is cloned is loaded with 200 request (not from cache)!

It there anyway for me to clone these objects without the browser sending a request to the server to load into DOM? Just totally on client side? The data is already written once in the HTML.

I would prefer not to send any requests to the server at all to clone these objects. Since it clones over 50, that's 50 requests being sent to the server in a second. Overloading it.

var increment = 0;
$("img").each(function(i) { //loop for each item
    if (increment > 4){
        increment = 0;  
    }

    if (i != 0) {  //only clone if there are more than one 'img' items      
        $("#hover-" + increment)  //item to clone     
          .clone(true)
          .attr("id", "beep-" + i)  //new name of object
          .appendTo($(this).parent()); 
    }

    $(this).data("instance", i);  //save the integer for loop
    increment=Math.floor(Math.random()*3);  //change increment
})

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

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

发布评论

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

评论(1

帅气称霸 2024-12-15 20:33:32

每个图像/声音文件引用都将在对服务器的请求中解析,除非图像已缓存在浏览器中。您可以设置“过期”和“过期”。图像的“Cache-Control”标头可强制缓存。当您请求图像时,这些标头会从服务器发送。

另一种选择是使用 Memcache 之类的缓存图像服务器端。这样,每个图像引用仍将解析到服务器,但图像将缓存在服务器端以便更快地访问。

有关详细信息,请参阅 - 缓存类型、Web 缓存如何工作、如何(以及如何不)控制缓存 http:// /www.mnot.net/cache_docs/

Each image/sound file reference will resolve in a request to the server unless the image is cached in the browser. You can set "Expires" & "Cache-Control" headers of your image to force caching. These headers are sent from the server when you image is requested.

Another option is to cache the image server side using something like Memcache. This way each image reference will still resolve to the server, but the image will be cached server side for faster access.

For more info see- Kinds of Caches, How Web Caches Work, How (and how not) to Control Caches http://www.mnot.net/cache_docs/

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