Javascript 知道特定资源何时完成加载
很抱歉,如果此线程被重新设置,但我在浏览器中找不到它 我想创建/处理一个事件,让我知道何时将特定资源加载到网页中。(类似于onload或domcontentload,但我需要知道每个资源何时完成,而不是整个页面) 示例-->资产=图像1、图像2、图像3 当完成加载image1时触发就绪事件 就绪事件触发后,开始加载映像2 继续…继续 在最后一个映像之后,再次触发就绪事件Javascript 知道特定资源何时完成加载,javascript,jquery,callback,event-handling,onload,Javascript,Jquery,Callback,Event Handling,Onload,很抱歉,如果此线程被重新设置,但我在浏览器中找不到它 我想创建/处理一个事件,让我知道何时将特定资源加载到网页中。(类似于onload或domcontentload,但我需要知道每个资源何时完成,而不是整个页面) 示例-->资产=图像1、图像2、图像3 当完成加载image1时触发就绪事件 就绪事件触发后,开始加载映像2 继续…继续 在最后一个映像之后,再次触发就绪事件 最终结果与DOMCONTENTHOST或“Windows .NoLoad”相同,但正如我所说的,我不想在资产负载的中间放置一些
最终结果与DOMCONTENTHOST或“Windows .NoLoad”相同,但正如我所说的,我不想在资产负载的中间放置一些逻辑。
有人知道怎么处理这种事情吗 我不确定这是否适用于您,但您是否尝试过setTimeOut函数 您可以设置加载图像的特定时间(通过ajax,您可以调用资源并等待request.done),然后调用下一个 我在这里还发现了与您的请求“类似”的内容:
如果这对您没有帮助,很抱歉。我不确定这是否适用于您,但您是否尝试过设置超时功能 您可以设置加载图像的特定时间(通过ajax,您可以调用资源并等待request.done),然后调用下一个 我在这里还发现了与您的请求“类似”的内容: 如果这对您没有帮助,很抱歉。我不确定您为什么(或是否)要按特定顺序“加载”图像。请注意,浏览器很少打开单个连接。通常会收集所有资源(在下载html本身之后),浏览器将并行加载其中的许多资源。简而言之-如果您这样做是为了提高性能或速度,那么您会减慢流程强> 更常见的延迟加载图像的方法是使用viewport/scroll位置来决定“下一步”应该加载哪些图像,有一些jquery插件,例如 无论如何-如果您不关心顺序,您只想在准备就绪时执行特定于元素的回调,您可以执行以下操作:
$("img").one("load", function() {
var $this = this;
// 'this' is your specific element
// do whatever you like to do onready
}).each(function() {
// handle cached elements
if(this.complete) $(this).load();
});
如果您确实关心顺序,并且确实希望在第一张图像准备好后加载下一张图像,则需要另一种方法
第一:HTML不包含图像源,但包含具有数据属性的图像:
第二:在JS中,你收集了所有这些没有真实来源的图像,你对收集进行排序,最后触发一个接一个的加载
var toLoad = [];
// scanning for all images with a data-src attribute
// and collect them in a specified order.
$('img[data-src]').each(function() {
// either you define a custom order by a data-attribute (data-assets-order)
// or you use the DOM position as the index. Mixing both might be risky.
var index = $(this).attr('data-assets-order') ?
$(this).attr('data-assets-order') : toLoad.length;
// already existing? put the element to the end
if (toLoad[index]) { index = toLoad.length; }
toLoad[index] = this;
});
// this method handles the loading itself and triggers
// the next element of the collection to be loaded.
function loadAsset(index) {
if (toLoad[index]) {
var asset = $(toLoad[index]);
// bind onload to the element
asset.on("load", function() {
// in case it is ready, call the next asset
if (index < toLoad.length) {
loadAsset(index + 1);
}
});
// set the source attribut to trigger the load event
asset.attr('src', asset.attr('data-src'));
}
}
// we have assets? start loading process
if (toLoad.length) { loadAsset(index); }
var-toLoad=[];
//扫描具有数据src属性的所有图像
//并按指定的顺序收集它们。
$('img[data src]')。每个(函数(){
//通过数据属性(数据资产顺序)定义自定义顺序
//或者使用DOM位置作为索引。混合使用两者可能有风险。
var index=$(this.attr('data-assets-order')?
$(this.attr('data-assets-order'):toLoad.length;
//已存在?将元素放在末尾
if(toLoad[index]){index=toLoad.length;}
toLoad[索引]=此;
});
//此方法处理加载本身和触发器
//要加载的集合的下一个元素。
函数loadAsset(索引){
如果(装载[索引]){
var资产=$(toLoad[指数]);
//将onload绑定到元素
asset.on(“加载”,函数(){
//如果准备就绪,请调用下一个资产
if(索引<装载长度){
负荷资产(指数+1);
}
});
//设置源属性以触发加载事件
asset.attr('src',asset.attr('data-src');
}
}
//我们有资产吗?开始加载过程
if(toLoad.length){loadAsset(index);}
我不确定您为什么(或是否)要按特定顺序“加载”图像。请注意,浏览器很少打开单个连接。通常会收集所有资源(在下载html本身之后),浏览器将并行加载其中的许多资源。简而言之-如果您这样做是为了提高性能或速度,那么您会减慢流程强>
更常见的延迟加载图像的方法是使用viewport/scroll位置来决定“下一步”应该加载哪些图像,有一些jquery插件,例如
无论如何-如果您不关心顺序,您只想在准备就绪时执行特定于元素的回调,您可以执行以下操作:
$("img").one("load", function() {
var $this = this;
// 'this' is your specific element
// do whatever you like to do onready
}).each(function() {
// handle cached elements
if(this.complete) $(this).load();
});
如果您确实关心顺序,并且确实希望在第一张图像准备好后加载下一张图像,则需要另一种方法
第一:HTML不包含图像源,但包含具有数据属性的图像:
第二:在JS中,你收集了所有这些没有真实来源的图像,你对收集进行排序,最后触发一个接一个的加载
var toLoad = [];
// scanning for all images with a data-src attribute
// and collect them in a specified order.
$('img[data-src]').each(function() {
// either you define a custom order by a data-attribute (data-assets-order)
// or you use the DOM position as the index. Mixing both might be risky.
var index = $(this).attr('data-assets-order') ?
$(this).attr('data-assets-order') : toLoad.length;
// already existing? put the element to the end
if (toLoad[index]) { index = toLoad.length; }
toLoad[index] = this;
});
// this method handles the loading itself and triggers
// the next element of the collection to be loaded.
function loadAsset(index) {
if (toLoad[index]) {
var asset = $(toLoad[index]);
// bind onload to the element
asset.on("load", function() {
// in case it is ready, call the next asset
if (index < toLoad.length) {
loadAsset(index + 1);
}
});
// set the source attribut to trigger the load event
asset.attr('src', asset.attr('data-src'));
}
}
// we have assets? start loading process
if (toLoad.length) { loadAsset(index); }
var-toLoad=[];
//扫描具有数据src属性的所有图像
//并按指定的顺序收集它们。
$('img[data src]')。每个(函数(){
//通过数据属性(数据资产顺序)定义自定义顺序
//或者使用DOM位置作为索引。混合使用两者可能有风险。
var index=$(this.attr('data-assets-order')?
$(this.attr('data-assets-order'):toLoad.length;
//已存在?将元素放在末尾
if(toLoad[index]){index=toLoad.length;}
toLoad[索引]=此;
});
//此方法处理加载本身和触发器
//要加载的集合的下一个元素。
函数loadAsset(索引){
如果(装载[索引]){
var资产=$(toLoad[指数]);
//将onload绑定到元素
asset.on(“加载”,函数(){
//如果准备就绪,请调用下一个资产
if(索引<装载长度){
负荷资产(指数+1);
}
});
//设置源属性以触发加载事件
asset.attr('src',asset.attr('data-src');