javascript Call function 10 times with 1 second between

前端 未结 8 1718
花落未央
花落未央 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:16

    You can try to use setInterval and use a variable to count up to 10. Try this:

    var number = 1;
    function oneSecond () {
      if(number <= 10) {
        // execute code here..
        number++;
      }
    };
    

    Now use the setInterval:

    setInterval(oneSecond, 1000);
    

提交回复
热议问题