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

 

senocular.com ActionScript Library

sendInterval.as

Name: sendInterval - setInterval with interval id passed to called function
Author: senocular: www.senocular.com
Date: 1899-12-31T00:54:28.900
Documentation:
global SENDINTERVAL: a setInterval that adds the current
interval id onto the arguments of the called function. This
way you can call the same function many times in multiple
intervals and still be able to access a unique ID relating
to the interval that currently called that function.  The
interval call is added on the end of the arguments array so
you would need to either allow for that in the function 
definition or use arguments[arguments.length-1] to access
the interval ID (which can then be cleared if need be).

Arguments / Returns:
- same as setInterval (I'm lazy ;)

Example:
 function go(arg, currID){
	trace(arg);
	if (arg == "once") clearInterval(currID);
}
sendInterval(go,500,"once");
sendInterval(go,500,"forever");

// output:
once
forever
forever
forever
forever

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
_global.sendInterval = function(a,b,c, args){
	// for a basic function call:
	if (typeof a == "function"){
		args = arguments.slice(2);
		var ID, func = function(){
			a.apply(null, args);
		}
		ID = setInterval(func, b, args);
	// for an object method call:
	}else{
		args = arguments.slice(3);
		var ID, func = function(){
			a[b].apply(a, args);
		}
		ID = setInterval(func, c, args);
	}
	return args[args.length] = ID;
}