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?
Asked
Active
Viewed 206 times
-1
-
1You can do it with vanilla JavaScript, without using jQuery. In fact, I don't even know how jQuery would help with this. – VLAZ Mar 15 '20 at 23:13
-
Could you please give me an example code on how to do this? – Slam Droid Mar 15 '20 at 23:14
-
`foo(); setTimeout(() => foo(), 1000)` – VLAZ Mar 15 '20 at 23:15
-
This answer can help you: https://stackoverflow.com/questions/21648264/javascript-call-function-10-times-with-1-second-between. As mentioned before it's written with plain JS, but there is no reason why you can't use it along with your jQuery code. – artist_dev Mar 15 '20 at 23:16
-
jquery is a framework. This is not a framework question. you can apply this TO various things, such as jquery. I think that this should be edited to either remove jquery, or add a sample which involves it. Also, where is your attempt? – Fallenreaper Mar 15 '20 at 23:19
1 Answers
1
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

Murtaza JAFARI
- 674
- 7
- 10