我有以下问题。我创建了一个类,在它的构造函数中加载了几个图像,我需要延迟其余的初始化,直到它们完全加载。我使用的代码如下(从构造的摘录):JS事件处理程序是否可以中断另一个处理程序的执行?
var self = this;
var imagesToLoad = 4;
var finishInit = function() {
alert(imagesToLoad);
imagesToLoad--;
if (imagesToLoad == 0) {
// remaining initializations
}
}
this._prevIcon = new Image();
this._prevIcon.onload = finishInit;
this._prevIcon.src = "img1.png";
this._nextIcon = new Image();
this._nextIcon.onload = finishInit;
this._nextIcon.src = "img2.png";
this._pauseIcon = new Image();
this._pauseIcon.onload = finishInit;
this._pauseIcon.src = "img3.png";
this._playIcon = new Image();
this._playIcon.onload = finishInit;
this._playIcon.src = "img4.png";
让我惊讶的,当被执行时显示的对话框在Firefox 7和4,3,2,4读4,4,4,4在IE 9中。我很困惑,因为我相信JS是单线程的,因此一个事件处理程序不应该打断另一个。 (或我的错误)在哪里?
在此先感谢。 此致敬礼, Tomek
尝试在行'src'和'onload'之间切换。 – CamelCamelCamel