Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

javascript - Long operations crash Office addin (JS)

I just created a (JS) Word Add-in and found that long synchronous operations can make it crash. In these cases, the following error is displayed - [ADD-IN ERROR Sorry, we had to restart because this add-in wasn't responding.]

enter image description here

The following code is ran on a button click.

    function scanText() {

        Word.run(function (context) {
            var body = context.document.body;

            context.load(body, 'text');

            return context.sync().then(function () {
                
                var r = thisOperationCanTakeALongTimeIfDocIsLarge(body.text);

            });
        })
        .catch(errorHandler);
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have finally found a good way to solve this... I use a WebWorker like so:

    function scanText() {
        var w;

        if (typeof (w) == "undefined") {
            w = new Worker("./Scripts/myWebWorker.js");
        }
        else
        {
            showNotification("Sorry! No Web Worker support.");
        }

        w.onmessage = function (event) {
            showNotification(event.data);
        };

        Word.run(function (context) {
            var body = context.document.body;

            context.load(body, 'text');

            return context.sync().then(function () {
                w.postMessage(body.text);
            });
        })
        .catch(errorHandler);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...