Versions Compared
compared with
Key
- This line was added.
- This line was removed.
- Formatting was changed.
Excerpt |
---|
Invoke a function n times... |
Syntax
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
(<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 / Value | Type | Mandatory | Description | API Version |
---|---|---|---|---|
<n> | Number | 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 | |
fn | Function | 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 | |
context | Object | The context in which the fn function should run. Default: The global object. | 1.1 |
Return value
Value | Type | Notes | API Version |
---|---|---|---|
<n> | Number | The number object, eg. so you can chain additional function iterations like so:
| 1.01 |
<error> | Error | An 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 | ||||||
---|---|---|---|---|---|---|
| ||||||
// 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 | ||||||
---|---|---|---|---|---|---|
| ||||||
(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 | ||||||
---|---|---|---|---|---|---|
| ||||||
Availability
Requires:
|
Div | ||||
---|---|---|---|---|
| ||||
ContentsJump to:
|
Div | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
GlobalsTopics:
|