setState is updating immediately even after using setTimeOut

前端 未结 3 1348
孤独总比滥情好
孤独总比滥情好 2021-01-24 02:13

I have a div with 10 elements, which are to be updated one-by-one with a time delay of say 2secs. Below is the code for the same

for(let boxNo=0; boxNo<10; bo         


        
3条回答
  •  粉色の甜心
    2021-01-24 03:00

    This works for fine for me.

     (function loop (boxNo) {          
      setTimeout(function () {   
         nodes[boxNo].isMarked = true;
         this.setState({nodes});                
         if (--boxNo) loop(boxNo);    // decrementing i and calling loop function again if i > 0
      },  ((boxNo+1)*2000)+boxNo)
    })(10);
    

提交回复
热议问题