const normal =useMemo(()=>{return(color:string, delay:number)=>{returnnewPromise((res)=>{setCurColor(color);setTimeout(()=>{res("");}, delay);});};},[]);
然后是闪烁函数,这里闪烁我就配置成1秒用black颜色闪烁一下:
const twinkle =useMemo(()=>{return(color:string, delay:number)=>{returnnewPromise((res)=>{let cur = color;let flag =true;let timer =setInterval(()=>{if(flag){if(cur === color){
cur ="black";setCurColor("black");}else{
cur = color;setCurColor(color);}}},1000);setTimeout(()=>{
flag =false;
window.clearTimeout(timer);res("");}, delay);});};},[]);
最后,useEffect里执行for循环,执行完毕后,进行递归:
useEffect(()=>{const fn =async()=>{for(let i in props.lights){const cur = props.lights[i];const normaltime = cur.duration - cur.twinkleDuration;awaitnormal(cur.color, normaltime);awaittwinkle(cur.color, cur.twinkleDuration);}fn();};fn();},[normal, props.lights, twinkle]);