简体   繁体   English

如何保持直到另一个功能完成?

[英]How to hold until another function is completed?

I'm work on a pixi program with wechat which many functions cannot work so I write by myself. 我正在使用微信开发一个pixi程序,其中许多功能无法使用,所以我自己写。

There are 3 functions: 有3个功能:

export function load(){
   PIXI.loader.add("images/bg.jpg").load(setup);
}

when the load() has completed setup() will be called. load()完成后,将调用setup()

  export const bg;
        function setup(){
        bg=new Sprite(resources["images/bg.jpg"].texture);
    }

in the main function I want to use the bg when the setup function is completed. 在主要功能中,我想在setup功能完成后使用bg

the first and second function is in file1.js.And in file2.js which is the main file includes functions: 第一个和第二个函数在file1.js中。而在file2.js中,该文件是包含功能的主要文件:

import * as global from "js/file1.js"
function main(){
    global.load();
    /* is there anyway to wait the setup is completed?*/
    bg.scale.set(0.5);
    /* since the load function completed but the setup is not complete so 
    I cannot use the bg.
}

I do not want to use setTimeOut to ensure the setup is gone. 我不想使用setTimeOut来确保设置已消失。 Is there anyway to wait? 反正还有等待吗?

I wonder if there could be a singal to the main function? 我想知道main功能是否单调? because I will start the load() function in the main() function and wait setup() is completed and I can use the objects initialed in the setup() function. 因为我将在main()函数中启动load()函数,并等待setup()完成,并且可以使用在setup()函数中setup()的对象。

Yes you can do it with async and await feaatures of javascript. 是的,您可以使用asyncawait javascript的功能。 An async function returns when the async task finishes. 当异步任务完成时,异步函数将返回。 So for you your code should be like this: 因此,对于您来说,您的代码应如下所示:

 async function setup(){
    bg = await new Sprite(resources["images/bg.jpg"].texture);
}

In that way a timeout is not needed and this is the correct async way in general. 这样就不需要超时,这通常是正确的异步方式。

Hope it helps. 希望能帮助到你。

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

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