简体   繁体   中英

How to execute a function twice after a period of time with jQuery?

I need to execute a function foo() twice and I need a wait interval between the first and the second execution. How can I do this with jQuery?

You can use setInterval for repeated execution with intervals and then clearInterval after 2 invocations:

callfunction();
var callCount = 1;
var repeater = setInterval(function () {
  if (callCount < 2) {
    callfunction();
    callCount += 1;
  } else {
    clearInterval(repeater);
  }
}, 1000);

This answer will help you: javascript Call function 10 times with 1 second between

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM