如何处理requireJs超时错误? [英] How to handle requireJs timeout error?

查看:84
本文介绍了如何处理requireJs超时错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用require.js作为我的加载框架的移动混合应用程序。我有加载错误的问题。我正在尝试做的是在设备离线时设置后备解决方案,我无法下载需要在屏幕上显示地图的google maps API脚本。我得到的只是

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

但我无法发现此错误并提供替代实施。这是我的gmaps模块定义

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;
});

我该怎么办?

编辑

由于您的帮助,我设法找到了可能的解决方案。我已经设置了这样的要求

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只是一个简单的模块:

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?

编辑2:

原谅我,它没有合作这个代码。它总是回到替代实现,因为gmaps需要使用异步插件来完全加载ed,我无法使其与插件一起使用。

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.

推荐答案

您可以通过以下方式捕获错误:

You can catch the error by :

requirejs.onError = function (err) {
    if (err.requireType === 'timeout') {
        alert("error: "+err);
    } 
    else {
        throw err;
    }   
};

希望这有帮助!

这篇关于如何处理requireJs超时错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆