Javascript 加载图像后继续函数
我是JS的初学者。我有一个在引导转盘中延迟加载图像的功能。加载图像后,如何继续此功能Javascript 加载图像后继续函数,javascript,twitter-bootstrap,carousel,Javascript,Twitter Bootstrap,Carousel,我是JS的初学者。我有一个在引导转盘中延迟加载图像的功能。加载图像后,如何继续此功能 $("#slider").on("slid.bs.carousel", function() { var nextItem = $("#slider").find("div.active").next(".item"); if(!nextItem.length) { nextItem = $(".active.item").siblings(":first");
$("#slider").on("slid.bs.carousel", function()
{
var nextItem = $("#slider").find("div.active").next(".item");
if(!nextItem.length)
{
nextItem = $(".active.item").siblings(":first");
}
var nextImage = nextItem.find("img");
if(nextImage.hasClass("lazy-load"))
{
var src = nextImage.attr("data-src");
nextImage.removeClass("lazy-load");
nextImage.attr("src", src);
}
//the following code must be executed after nextImage is loaded:
clearTimeout(t);
$("#slider").carousel("pause");
var duration = $(this).find("div.active").attr("data-interval");
t = setTimeout(function() { $("#slider").carousel({interval: 1000}); }, duration-1000);
});
只需等待onload事件:
$("#slider").on("slid.bs.carousel", function()
{
var that = this;
var nextItem = $("#slider").find("div.active").next(".item");
if(!nextItem.length)
{
nextItem = $(".active.item").siblings(":first");
}
var nextImage = nextItem.find("img");
if(nextImage.hasClass("lazy-load"))
{
var src = nextImage.attr("data-src");
nextImage.removeClass("lazy-load");
nextImage.attr("src", src);
}
//the following code must be executed after nextImage is loaded:
$(nextImage).on("load",function(){
clearTimeout(t);
$("#slider").carousel("pause");
var duration = $(that).find("div.active").attr("data-interval");
t = setTimeout(function() { $("#slider").carousel({interval: 1000}); }, duration-1000);
});
});