如何处理requireJs超时错误?

编程入门 行业动态 更新时间:2024-10-11 05:30:36
本文介绍了如何处理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!maps.googleapis/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!maps.googleapis/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: ['maps.googleapis/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超时错误?

本文发布于:2023-11-25 23:27:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631775.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何处理   错误   requireJs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!