在图像元素改变了src之后,计时函数的运行时间

Timing a function to run after an image element has changed its src

本文关键字:函数 运行时间 之后 src 图像元 图像 元素 改变      更新时间:2023-09-26

我正在改变一些图像的src属性。加载新图像后,我想运行一个名为wheelBuilder.rebuildColors()的函数

$images.each(function() {
    $(this).attr("src", $(this).attr("data-original-front-src"));
});
wheelBuilder.rebuildColors();

这段代码在某些浏览器(chrome, safari, ie)上运行良好,但在Mozilla firefox上不行。

是否有一种方法可以设置它在所有图像加载后运行该函数?

谢谢

试试这个。必须在更改src

之前设置函数。
var total = $images.length,loaded=0;
function testRebuild() {
  if (loaded==total) wheelBuilder.rebuildColors();
}
$images.each(function() {
  this.onload=function() {
    loaded++; testRebuild();
  }
  this.onerror=function() {
    $(this).hide();
    loaded++; testRebuild();
  }
  $(this).attr("src", $(this).attr("data-original-front-src"));
});