简体   繁体   English

如何在Javascript中阻止功能并等待其他任务完成

[英]How to block function and wait for other task to finish in Javascript

I have my own Javascript function (class) in which I do some drawings on canvas.我有我自己的 Javascript 函数(类),我在画布上画了一些画。

At one point of developing I got a problem with async tasks, because JS won't allow for any type of managing it.在开发过程中,我遇到了异步任务的问题,因为 JS 不允许对其进行任何类型的管理。

My code looks like this:我的代码如下所示:

function imageEditor(canvas, image, options) {
    var that = this;

    this.getImageData = function (format, encoderOptions) {
        var animState = that._waitAnim.isOn;  //saving animation state

        that.waitingAnimation(false);  //turning off animation
        while (that._waitAnim.isRunning) { imgEdit.waitingAnimation(false); }  //waiting for wait animation to finish rendering it's image (this locks JS)

        that._canvasRedraw();  //performing our canvas redraw
        var imgData = that._canvas.toDataURL(format, encoderOptions);

        that.waitingAnimation(true);  //turning on animation again

        return imgData;
    };

    this.waitingAnimation = function (startStop) {
        if (typeof startStop === 'boolean') {
            if (startStop && that._waitAnim.isRunning)
                return;

            that._waitAnim.isOn = startStop;

            if (startStop) {
                that._waitAnim.isRunning = true;
                window.requestAnimationFrame( that._waitingAnimation );
            }
        }

        return that;
    };

    this._canvasRedraw = function () {
        //performing image redrawing on canvas (not a waiting indicator)
    };

    this._waitingAnimation = function () {
        //performing waiting indicator drawing

        if (!that._waitAnim.isOn || !that._waitAnim.isRunning) {  //it's time to finish our animation
            that._waitAnim.isOn = false;
            that._waitAnim.isRunning = false;
            that._canvasRedraw();
            return;
        }

        window.requestAnimationFrame(that._waitingAnimation);  //schedule next indicator redraw
    };
}

The problem is that this line is blocking whole Javascript:问题是这一行阻塞了整个 Javascript:

while (that._waitAnim.isRunning) { imgEdit.waitingAnimation(false); }

I wanted to wait for indicator redraw to finish it's task and than draw my image on canvas, export to DataURL, than restore indicator, and return DataURL to user.我想等待指标重绘完成它的任务,然后在画布上绘制我的图像,导出到 DataURL,然后恢复指标,并将 DataURL 返回给用户。

Is there any possible way to do this without changing whole code?有没有办法在不改变整个代码的情况下做到这一点?

You can't make asynchronous functions block.你不能让异步函数阻塞。

Your attempt blocks everything, including the callback of the asynchronous function that you are waiting for.您的尝试阻止了一切,包括您正在等待的异步函数的回调。

If you want to do something after an asynchronous function finishes, then put that something in a function and use it as a callback.如果你想在异步函数完成后做一些事情,那么把它放在一个函数中并将其用作回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM