使用requirejs timeout加载异步资源 [英] Load async resource with requirejs timeout

查看:88
本文介绍了使用requirejs timeout加载异步资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用requirejs和async插件加载适用于JavaScript的Google API客户端库:

I tried to load the Google APIs Client Library for JavaScript with requirejs and the async plugin:

require.config({
    paths : {
        async : '../lib/requirejs/async'
    },
    waitSeconds: 60
});

define('gapi', ['async!https://apis.google.com/js/client.js!callback'],
    function(){
        console.log('gapi loaded');
        return gapi.client;
    }
);

require(['gapi'], function(){
    console.log("Callback");
    console.log(gapi);
});

加载此库

<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>

所有内容都在不到2秒内加载但我总是收到此错误:

Everything is loaded in less than 2s but I always get this error:

Uncaught Error: Load timeout for modules: async!https://apis.google.com/js/client.js!callback_unnormalized2,async!https://apis.google.com/js/client.js!callback
http://requirejs.org/docs/errors.html#timeout 


推荐答案

TL; DR; 更改!回调!onload 应修复超时。

define('gapi', ['async!https://apis.google.com/js/client.js!onload'],
    function(){
        console.log('gapi loaded');
        return gapi.client;
    }
);

之后的值用作异步回调的参数名称,在这种情况下,加载的URI将类似于 https://apis.google.com/js/client.js?onload=__async_req_3__ 其中 __ async_req_3 __ 是加载Google API时触发的全局变量(回调函数)(通知插件已满足所有依赖项)。

The value after the ! is used as the argument name for the async callback, in this case the URI loaded will be something like https://apis.google.com/js/client.js?onload=__async_req_3__ where __async_req_3__ is a global variable (callback function) triggered as soon as the Google API is loaded (notifies the plugin that all dependencies are met).

这篇关于使用requirejs timeout加载异步资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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