How to handle requireJs timeout error?
javascript, requirejs
Solution
You can catch the error by :
requirejs.onError = function (err) {
if (err.requireType === 'timeout') {
alert("error: "+err);
}
else {
throw err;
}
};
Hope this helps!
Problem
I'm writing a mobile hybrid app using require.js as my loading framework. I have an issue with loading errors. What I'm trying to do is setup a fallback solution when the device is offline and I can't download the google maps API script that I need to display a map on the screen. All that I get is ``` Uncaught Error: Load timeout for modules: async!http://maps.googleapis.com/maps/api/js?sensor=true ``` but I'm not able to catch this error and provide an alternative implementation. Here is my gmaps module definition ``` define('gmaps', ['async!http://maps.googleapis.com/maps/api/js?sensor=true'],function(){ return window.google.maps; }); ``` What can I do? EDIT I managed to find a possible solution thanks to your help. I've setup require like this ``` require.config({ paths: { gmaps: ['http://maps.googleapis.com/maps/api/js?sensor=true', 'lib/dummymaps'] } } ``` dummymaps is only a simple module: ``` define({ dummy: true }); ``` Then in my "parent" module I do: ``` define(["gmaps"],function(gmaps){ ... if(typeof gmaps.dummy != 'undefined' && gmaps.dummy == true){ // use a local image as map } else { // initialize google maps canvas } }); ``` Do you think that's a good solution? EDIT 2: Forgive me, it's not working with this code. It's always falling back to the alternative implementation because gmaps needs to use async plugin to be fully loaded and I'm not able to make it work with the plugin.