目录
所查找的对象可能为空
用 if 或者 ? 解决 或者 !如果获取道德东西确定有,哟个!规避错误
//用async+await+new Promise关键字实现依次异步操作
//用async+await+new Promise关键字实现依次异步操作
let a = infoRect.getChildByName('callPanel') as BABYLON.GUI.StackPanel;
for(let i=0;i<a.children.length;i++){
let b = a.children[i] as BABYLON.GUI.Rectangle;
b.background = "yellow";
console.log(1);
await new Promise(async(reslove)=>{
setTimeout(()=>{reslove(null)},timeparagraph[i])
})
console.log(2);
b.background = "#FFFFFF00";
}
return
//forEach和map在内部用await无效,会同步执行
a.children.forEach(async(element, index) => {
let b = element as BABYLON.GUI.Rectangle;
b.background = "yellow";
console.log(1);
await new Promise(async(reslove)=>{
setTimeout(()=>{reslove(null)},timeparagraph[index])
})
console.log(2);
b.background = "#FFFFFF00";
/* setTimeout(() => {
b.background = "#FFFFFF00";
},timeparagraph[index]); */
});
promise结构
匿名函数
回调函数如果是匿名函数this全部为window
如果是箭头函数,最近外层查找对象