Google platform.js有时不触发onload回调

时间:2018-11-15 15:17:10

标签: javascript google-oauth

如果我以特定顺序加载其他JavaScript源,则以下内容将不会调用pl​​atform_callback。

<script type="text/javascript" src="//apis.google.com/js/platform.js?onload=platform_callback" async defer></script> 
<script>
    function platform_callback() {
        console.log("callback");
    }
</script>

我很难复制此代码...但是,如果在调用平台后加载了很多js库(角,矩,引导程序),则不会触发onload回调。

这是一个gif,它显示console.log在刷新大约12次后仅在某些时间触发: https://i.imgur.com/C5FFJhb.gif

此html会导致问题:

<script src="https://apis.google.com/js/platform.js?onload=platform_callback" async defer></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular-animate.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular-route.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular-sanitize.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js" crossorigin="anonymous"></script>
<script>
    function platform_callback() {
        console.log("init");
        gapi.load('auth2', function() {
            console.log("auth2");
        });
    }
</script>

1 个答案:

答案 0 :(得分:2)

加载google库,然后调用platform_callback,如果浏览器正忙于加载其他js文件,则该时间可能未定义。