Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

Invoke a function n times...

 

Syntax

Code Block
themeRDark
languagejavascript
linenumberstrue
(<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.01
<error>ErrorAn error, most likely invalid parameters or invalid syntax (eg. not wrapping a primitive value in brackets or using 2 dots).1.01

Examples

Code Block
themeRDark
languagejavascript
linenumberstrue
// 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
} );
Code Block
themeRDark
languagejavascript
linenumberstrue
(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);
});
Div
classbox
Availability
Status
colourYellow
titleBeta

Requires:

Div
classbox
Contents

Jump to:

Table of Contents
maxLevel5

Div
classbox

Globals

Topics:

Child pages (Children Display)
alltrue
depthall
pageGlobals
excerpttrue