Javascript: Class for detecting if images and fonts aer ready
Thu Feb 08 2024 00:08:49 GMT+0000 (Coordinated Universal Time)
Saved by
@marcopinero
#javascript
class TLoadComplete{
constructor( callbackFunc, frequency ){
this.callback = callbackFunc || (()=>{ console.log("completed")});
this.frequency = frequency || 500;
}
start(){
if (this.started) return;
this.started = true;
let me = this;
this.images = [...document.images];
this.interval = setInterval(()=>{
let result = me.images.filter(el => !el.complete).length == 0 && document.fonts.ready;
if (result) {
me.callback();
console.log("completed");
me.stop();
}
}, me.frequency)
}
stop(){
clearTimeout(this.interval);
this.started = false;
}
}
content_copyCOPY
Comments