Last Updated: February 25, 2016
·
2.757K
· reaktivo

Google's WebFont loader .ready()

Google WebFont config allows you assign a active function that will be executed when all fonts are loaded. The problem is you can only assign one function. I wrote the ready method you can add to the WebFontConfig global allows you to add callbacks, without replacing the last one, in the same style as jQuery.

window.WebFontConfig = {
  google: { families: ['Oswald:400,300:latin'] },
  listeners: [],
  active: function() {
    this.called_ready = true;
    for(var i = 0; i < this.listeners.length; i++) {
      this.listeners[i]();
    }
  },
  ready: function(callback) {
    if (this.called_ready) {
      callback()
    } else {
      this.listeners.push(callback)
    }
  }
};

Then on another script:

WebFontConfig.ready(yourCallback);