簡體   English   中英

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

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

我正在使用微信開發一個pixi程序,其中許多功能無法使用,所以我自己寫。

有3個功能:

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

load()完成后,將調用setup()

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

在主要功能中,我想在setup功能完成后使用bg

第一個和第二個函數在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.
}

我不想使用setTimeOut來確保設置已消失。 反正還有等待嗎?

我想知道main功能是否單調? 因為我將在main()函數中啟動load()函數,並等待setup()完成,並且可以使用在setup()函數中setup()的對象。

是的,您可以使用asyncawait javascript的功能。 當異步任務完成時,異步函數將返回。 因此,對於您來說,您的代碼應如下所示:

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

這樣就不需要超時,這通常是正確的異步方式。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM