有没有办法在Office.js中禁用初始化计时器?

时间:2017-03-31 17:35:35

标签: office-js

在网络条件差的情况下测试我们的Office.js加载项时遇到了一个问题。甚至在加载脚本之前就会抛出以下错误:
Uncaught Office.js has not been fully loaded yet. Please try again later or make sure to add your initialization code on the Office.initialize function.

经过几个小时的调试后,我们在Office.js中找到了以下代码:

g.waitForFunction(function() {
    return Microsoft.Office.WebExtension.initialize != undefined
}, function(a) {
    if (a) {
        if (h.prepareApiSurface)
            Microsoft.Office.WebExtension.initialize(h.getInitializationReason(b));
        else
            h.prepareRightBeforeWebExtensionInitialize(b);
        h.prepareRightAfterWebExtensionInitialize && h.prepareRightAfterWebExtensionInitialize()
    } else
        throw "Office.js has not been fully loaded yet. Please try again later or make sure to add your initialization code on the Office.initialize function."
}, 400, 50)

waitForFunction方法有4个参数:
function - 返回布尔值的状态检查函数 function - 当状态检查函数返回true或方法失败时调用的回调函数 Number - 重试次数 Number - 尝试之间的毫秒数。

根据我们发现的源代码,在框架超时之前,Office加载项有大约20秒的时间进行初始化并且简单地放弃。这在Office 365联机和慢速连接上特别成问题。

以下加载项重现了该问题:
         

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <title>Test Init</title>
    <script src="//appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>
</head>

<body class="ms-font-l" style="width:100%;height:100%;overflow:hidden;position:relative;">
<div id="app" style="height:100%"></div>
<script>
    console.log('Our script loaded...');
    setTimeout(function () {
        Office.initialize = function () {
            console.log('Our App Initialized!!!')
        };
    }, 15000);
</script>
</body>

</html>

我的问题是,有没有办法绕过这个超时?或者至少增加它?也许使用类似的东西:
Office.initialize(function() { /* ... */ });
而不是:
Office.initialize = function() { /* ... */};

如果没有,办公室团队会考虑更改计时器而不是使用财产:

Object.defineProperty(Office, 'initialize', {
    get: function() { return Office._initialize; },
    set: function(value) { Office._initialize = value; Office.triggerCodeDependantOnInitialize(); }
});

1 个答案:

答案 0 :(得分:3)

目前无法避免超时。也就是说,在执行任何其他操作之前,应始终调用Office.initialize。一般来说,我的指导是将此调用放在HEAD的底部。

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <title>Test Init</title>
    <script src="//appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>
    <script>
        Office.initialize = function () {
            console.log('Office Initialized!!!')     

        };
    </script>
</head>

然后我使用一个事件来触发文档准备好后需要的任何其他初始化代码(即document.addEventListener("DOMContentLoaded", function(event){ })或JQuery&#39; $(document).ready