Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

repeat.as

Name: Function.prototype.repeat() - (MX) runs function repeatedly for a certain number of times
Author: senocular: www.senocular.com
Date: 1899-12-31T00:42:33.000
Documentation:
Function REPEAT (MX): repeats function over again for a
certain number of times all with the same arguments.  Only
the results from the last called instance of the function
is returned.
 
Arguments:
- args: (optional) an single value or array object of values to be used as arguments
in the function calls.  If not given, nothing is passed.
- n: number of times to repeat the function.
 
Returns:
- returns the return of the last function called in the succession.

Example:
function PrintDouble(n){
	trace (n+n);
	return "the end";
}
trace(PrintDouble.repeat(2,10)); // repeats PrintDouble(2) 10 times
// Output:
4
4
4
4
4
4
4
4
4
4
the end
// since only the last of the functions repeated gets its return... returned,
// the trace this call was in only gets that value (the end) 

1
2
3
4
5
6
7
Function.prototype.repeat = function(args, n){
	var i, r;
	if (arguments.length == 1) n = args;
	else if (typeof(args) != "Object") args = [args];
	for (i=0;i<n;i++) r = this.apply(null, args);
	return r;
}