changing interval of setInterval on frame entry

I’d like to constantly change the interval in a setInterval method based on a variable that changes based on user input. I.e., the frequency of starting a movieclip from an array of movieclips increases or decreases based on the variable. However, the clearInterval doesn’t stop it when the variable exceeds the desired threshold. I’m sure this make sno sense, but here is the code:

  this.addEventListener(Event.ENTER_FRAME, checkForAPs);

		public function checkForAPs(e:Event):void 
		{	
			if (Vm >= -40) 
				{
					apFreqCount = 13 + ((Vm + 10) / 3.1); 
					interval = (13 - apFreqCount) * 50; // interval is set between 650 and 50 ms based on the Vm
					myInterval = setInterval (startAP, interval)
				}
			else if (Vm < -40) clearInterval(myInterval);  // should clear the setInterval if Vm exceeds -40
		     }
		function startAP ():void
		{
			axonAPArray[axonAPIndex].play(); 
			axonAPIndex ++;	
			if (axonAPIndex > 23) axonAPIndex = 0;
		}

What is Vm and when does it change? If you’re checking this every frame, you’re potentially creating a new interval every frame if Vm is >= -40.

You should probably check Vm, then calculate interval, then only when the interval just calculated does not match the existing interval being used, call setInterval to update it. Additionally, any time you call setInterval, clearInterval before it to prevent duplicated intervals.

1 Like

Vm is constantly changing based on input from the keyboard. Yes - I create a new interval every frame entry that Vm is >= -40. The idea is that as the Vm goes from -40 and -10, new movie clips (axonAPArray[axonAPIndex]) are added at faster and faster rates.

BTW - if this help it make more sense, here is the not-quite-right version. You can see Vm displayed in the upper meter. APs are added to the axon on frame entry in this version, which is too fast and also not proportional to the magnitude over -40:

http://facweb.plattsburgh.edu/donald.slish/SummationGame/NeuronSummationGame.html

Good advice in second paragraph - will try this.