最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Load async resource with requirejs timeout - Stack Overflow

programmeradmin1浏览0评论

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!.js!callback'],
    function(){
        console.log('gapi loaded');
        return gapi.client;
    }
);

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

The usual way to load this library is

<script src=".js?onload=handleClientLoad"></script>

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

Uncaught Error: Load timeout for modules: async!.js!callback_unnormalized2,async!.js!callback
.html#timeout 

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./js/client.js!callback'],
    function(){
        console.log('gapi loaded');
        return gapi.client;
    }
);

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

The usual way to load this library is

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

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

Uncaught Error: Load timeout for modules: async!https://apis.google./js/client.js!callback_unnormalized2,async!https://apis.google./js/client.js!callback
http://requirejs/docs/errors.html#timeout 
Share Improve this question edited Aug 5, 2013 at 21:35 JJJ 33.2k20 gold badges94 silver badges103 bronze badges asked Aug 5, 2013 at 21:02 SydneySydney 12.2k21 gold badges92 silver badges145 bronze badges 2
  • Please where you able to get this working after optimization with r.js (requirejs optimizer)?, For me it works normally but after optimization it doesn't . – I.Tyger Commented Apr 8, 2015 at 23:31
  • ^^ Did you manage to fix it after optimization? – jssridhar Commented May 14, 2015 at 10:10
Add a ment  | 

1 Answer 1

Reset to default 9

TL;DR; change the !callback to !onload that should fix the timeout.

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

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./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).

发布评论

评论列表(0)

  1. 暂无评论