javascript Call function 10 times with 1 second between

前端 未结 8 1714
花落未央
花落未央 2021-01-24 17:40

How to call a function 10 times like

for(x=0; x<10; x++) callfunction();

but with 1 sec between each call?

8条回答
  •  生来不讨喜
    2021-01-24 18:24

    const functionCounterTimer = (callCount) => {
      if (callCount < 10) {
        setTimeout(() => {
          ++callCount
          console.log("Function Call ", callCount);
          functionCounterTimer(callCount);
        }, 1000);
      }
    }
    
    functionCounterTimer(0);

    The above was my approach to a similar question.

提交回复
热议问题