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

 

senocular.com ActionScript Library

setPInterval.as

Name: setPInterval [MX] sets pausable interval
Author: senocular: www.senocular.com
Date: 1899-12-31T00:20:47.300
Documentation:
global function SETPINTERVAL: like setInterval though the
returned ID is an object with two functions associated with
it, pause and resume, which lets you pause and resume the
interval call.  a clearPInterval can be used to clear the
interval alltogether.

Arguments:
- same as set interval

Returns:
- an "interval object" with pause and resume functions.  The actual interval ID is in a property
within this object aptly named "ID"  Using clearPInterval will clear this:

clearPInterval(mySetPIntervalReturnIntervalObject);

Example:
 function faucet(){ trace("drip"); }
drip = setPInterval(faucet, 500);

this.onMouseDown = function(){
	drip.pause(); // stops drip trace
}
this.onMouseUp = function(){
	drip.resume(); // restarts drip trace
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
_global.setPInterval = function(a,b,c, args){
	var run=true, func, intObj = {};
	intObj.pause = function(){ run = false; }
	intObj.resume = function(){ run = true; }
	intObj.toggle = function(){ run = !run; }
	if (typeof a == "function"){ // for a basic function call:
		args = arguments.slice(2);
		func = function(){
			if (run) a.apply(null, args);
		}
		intObj.ID = setInterval(func, b, args);
	}else{ // for an object method call:
		args = arguments.slice(3);
		func = function(){
			if (run) a[b].apply(a, args);
		}
		intObj.ID = setInterval(func, c, args);
	}
	return intObj;
}
_global.clearPInterval = function(intObj){
	clearInterval(intObj.ID);
}