Invoke a function n times...

 

Syntax

(<n>).times(fn[, context]); // use with primitive number or number object
 
<n>..times(fn[, context]); // only use with primitive number
 
<n>.times(fn[, context]); // only use with number object

Parameters / Values

Parameter / ValueTypeMandatoryDescriptionAPI Version
<n>Number(tick)

The number of times the fn function should be invoked.

If the number is 0, negative or NaN, the function will not be invoked.

1.1
fnFunction(tick)

The function to invoke.

The function is passed a single numeric argument, starting at n-1 on the first iteration, and decrementing with each subsequent iteration, ending at 0 on the final iteration.

Note: You can use  function.curry() to set parameters for the function. You can also use Hook API to modify arguments or retrieve results when the function is called.

1.1
contextObject(error)

The context in which the fn function should run.

Default: The global object.

1.1

Return value

ValueTypeNotesAPI Version
<n>Number

The number object, eg. so you can chain additional function iterations like so:

5..times(fnA).times(fnB)

1.1
<error>ErrorAn error, most likely invalid parameters or invalid syntax (eg. not wrapping a primitive value in brackets or using 2 dots).1.1

Examples

// assuming 4 player game...
(maxPlayers).times( function(num) {
	console(num); // 3, 2, 1, 0
	// want incremental order?
	console(maxPlayers - num - 1); // 0, 1, 2, 3
} );
(playerData.length).times(... // wat?!
 
// always use .forEach on arrays, much better as it
// gives you the value as well as the array index.
playerData.forEach( function(obj, id) {
	console("Player "+id+" is in team "+obj.team);
});
Availability

Requires:

Contents

Jump to:

Globals

Topics: