Ted Patrick > { Events & Community } > Adobe Systems


Fun with Intervals

There are several tricks to working with Intervals that makes them easy to manage and change.&

The ids for intervals are numbers globally in the player. When you create an interval with setInterval, it returns a unique id for the interval you set starting with 1 and iterating higher.

myInterval = setInterval(function(){trace('I1')},100)
trace(myInterval) //1

myInterval = setInterval(function(){trace('I2')},100)
trace(myInterval) //2

myInterval = setInterval(function(){trace('I3')},100)
trace(myInterval) //3

//clearing the intervals you have set it even easier.

clearInterval(1)
clearInterval(2)
clearInterval(3)


Another interesting patterns is creating a generic Interval function that you can add functions into. The following function will attempt to execute every object stored within the function.

intervalCore = function(){
var self = arguments.callee
for( var x in self) self[x]()
}
myInterval = setInterval(intervalCore,100)

intervalCore.one = function(){trace('one')}
intervalCore.two = function(){trace('two')}
intervalCore.three = function(){trace('three')}

//delete the items in the interval:

delete intervalCore.one
delete intervalCore.two
delete intervalCore.three


Or if you simply want to clear out all the Intervals within the Flash Player:

i = setInterval(function(){},100) + 1
while(i--){
clearInterval(i)
}


Cheers,

Ted ;)

5 Responses to “ Fun with Intervals ”

  1. # Anonymous Anonymous

    Great post! Nice to see cool stuff like this every now and again. Thanks for sharing, I will use this bit of code to some work, because I was felt that intervals where a pain to deal with.

    John  

  2. # Blogger Ted Patrick

    It might be nice to roll some of this functionality into a higher level Interval class to make intervals easier to work with.

    Interval.add( name, function , delay, props)
    Interval.remove( name )
    Interval.removeAll()
    Interval.start( name )
    Interval.startAll()
    Interval.stop( name )
    Interval.stopAll()
    Interval.list() //returns interval data

    Cheers, ted ;)  

  3. # Anonymous Anonymous

    Gret discovery on the auto incrementing globals. Removing all intervals should be a handy debugging technique.

    -will  

  4. # Anonymous Anonymous

    Kenny Bunch just released his IntervalManager class over at his blog. May be exactly what you mentioned in your previous comment...

    ~mRay  

  5. # Blogger Anthony

    You just absolutely made my day. That clear all intervals snippet was essential for a program I was running that would load the same flash file at various difficulties. You rock. :)  

Post a Comment



© 2008 Ted On Flash