Function start a certain number of times

function runAgain() { window.setTimeout(foo, 100); } function foo() { //Do somthing runAgain(); } 

I can use the above code to run the function an infinite number of times at intervals of one second.

What is the standard way to run a function a certain number of times. Suppose I want foo() executed 5 times with an interval of 1 second.

EDIT . He said that in Javascript global variables should be avoided. Isn't there a better way?

Using the answers, I created such a function: (Working example: http://jsbin.com/upasem/edit#javascript,html )

 var foo = function() { console.log(new Date().getTime()); }; var handler = function(count) { var caller = arguments.callee; //Infinite if (count == -1) { window.setTimeout(function() { foo(); caller(count); }, 1000); } if (count > 0) { if (count == 0) return; foo(); window.setTimeout(function() { caller(count - 1); }, 100); } if (count == null) {foo(); } }; handler(-1); //Runs infinite number of times handler(0); //Does nothing handler(2); //Runs two times handler(); //Runs foo() one time 
+7
source share
5 answers

Assuming you have a function:

 var foo = function() { ... }; 

or if you prefer:

 function foo() { ... } 

you can call it 5 times in 1 second intervals:

 (function(count) { if (count < 5) { // call the function. foo(); // The currently executing function which is an anonymous function. var caller = arguments.callee; window.setTimeout(function() { // the caller and the count variables are // captured in a closure as they are defined // in the outside scope. caller(count + 1); }, 1000); } })(0); 

And here is a live demonstration .

+5
source
 var counter = 1; function foo() { if (counter < 5){ counter++ window.setTimeout(foo, 1000); } } foo()// it will run 5 times; 

Live demo


Version with static variable:

 function foo() { if (typeof foo.counter == 'undefined') { foo.counter = 0; } alert("Run No. " + (++foo.counter)); if (foo.counter < 5) { setTimeout(function() { foo(foo.counter + 1); }, 400); } } foo(); 

Live demo


Version with hidden input

 function foo() { var counter = document.getElementById('counter'); var counterValue = parseInt(counter.value, 10); alert('Run No. ' + counterValue); if (counterValue< 5) { counter.value = counterValue + 1; window.setTimeout(foo, 400); } } foo();​ 

Live demo


Closed Version:

 var x = function() { var counter = 1; (function foo() { alert('Run No. ' + counter); if (counter < 5) { counter++; setTimeout(foo, 400); } })(); }; x();​ 

Live demo

+8
source

To avoid contamination of the global environment with additional variables, you can wrap it with an anonymous function:

 (function() { var counter = 0; function foo() { // do stuff if ((++counter) < 5) window.setTimeout(foo, 1000); } })(); 
+2
source

use a global variable and increment it in the foo () function to count the number of times it was called.

 var counter=0; function runAgain() { window.setTimeout(foo, 1000); } function foo() { //Do somthing if((++counter)<5) runAgain(); } 
+1
source

function call (func, arg) { return { func, arg, times: function (num) { let counter = 0; while (counter < num) { this.func(this.arg); counter += 1; } } }; }

Then, to use it, you can do

call(yourFunction).times(10)

or, if you need to enter an argument:

call(yourFunction, arg).times(10)

0
source

All Articles