[英]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()
的对象。
是的,您可以使用async
并await
javascript的功能。 当异步任务完成时,异步函数将返回。 因此,对于您来说,您的代码应如下所示:
async function setup(){
bg = await new Sprite(resources["images/bg.jpg"].texture);
}
这样就不需要超时,这通常是正确的异步方式。
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.