You're looking for media events, which says you could use e.g. loadeddata
.
I'd like to address some other points:
- Characters inside a regexp character group don't need escaping.
- Why not use jQuery for creating the elements and binding the event handler?
I altered your code a little bit:
$.each(assets, function() {
var r = /.([^.]+)$/,
ext = r.exec(this), //get file type
tmp = ext[1] === 'png'
? $("<img>")
: $("<audio>"),
eventName = ext[1] === 'png'
? 'load'
: 'loadeddata';
tmp
.on(eventName, function() {
var i = assets.indexOf(this);
assets.splice(i, 1);
if (!assets.length){
console.log('all loaded');
app.build();
}
})
.attr("src", this); // only set after adding the load callback
// to avoid a possible race condition
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…