I got to thinking... 

The problem with setting a single interval using setCurrentInterval is that
you can only have one interval running at a time, while the problem with
clearAllIntervals intervals is being forced to reset those you want to keep.


So I just wanted to share another idea, clearing all intervals EXCEPT those
you want to keep.


//
//      CLEAR ALL INTERVALS - EXCEPT
//

function clearAllIntervalsExcept ( a:Array  )
{
        var x = {};
        for ( var i in a ){ x [ '_' + a [ i ] ] = 1; };
        
        // create new interval
        var id = setInterval ( this, 'blank', 0 );
        while ( id != 0 )
        {
                // count through all intervals and clear them one by one
                // except those defined in the array
                if ( !x [ '_' + id ] ) clearInterval ( id );
                id--;
        }
}

function test ()
{
        trace ( arguments );
}

var _1 = setInterval ( this, 'test', 0, 1, 0 );
var _2 = setInterval ( this, 'test', 0, 2, 0 );
var _3 = setInterval ( this, 'test', 10, 3, 10 );
var _4 = setInterval ( this, 'test', 100, 4, 100 );
var _5 = setInterval ( this, 'test', 1000, 5, 1000 );

var exceptionArray = [ _4, _5 ];

clearAllIntervalsExcept ( exceptionArray ); // only _4 and _5 will run







_____________________________

Jesse Graupmann
www.jessegraupmann.com   
www.justgooddesign.com/blog/    
_____________________________




-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Jesse
Graupmann
Sent: Thursday, April 26, 2007 9:13 PM
To: flashcoders@chattyfig.figleaf.com
Subject: RE: [Flashcoders] Clear Set Interval Q:

@ Muzak

Clearing the previous interval works as expected. I'm showing the same
example with a little more padding to illustrate how it might be better used
inside another function - the benefit being the ability to still keep track
of the most current interval id.        

//
//      ONLY ONE INTERVAL
//
        
        function setCurrentInterval () 
        {
                var id = setInterval.apply ( null, arguments )
                clearInterval ( id - 1 );
                return id;
        }
        
        function something () 
        {
                trace( 'something' );   
        }
        
        var id = setCurrentInterval ( this, "something", 0 )
        var id = setCurrentInterval ( this, "something", 10 )
        var id = setCurrentInterval ( this, "something", 100 )
        var id = setCurrentInterval ( this, "something", 1000 )
        
        trace( 'ID: ' + id );



Personally I hate intervals, and I don't mind saying that. For all the
problems they cause due to scope issues and whatnot, but unfortunately you
can't avoid them if you really care about time. So I say just clear them
all.


//
//      CLEAR ALL INTERVALS
//

function clearAllIntervals ( )
{
        // create new interval
        var id = setInterval ( this, 'blank', 0 );
        while ( id != 0 ){
                // count through all intervals and clear them one by one
                clearInterval ( id )
                id--
        }
}
clearAllIntervals ( );



Through the course of this discussion about the never ending saga of
intervals, a few things beyond the last example stood out at me.

setInterval is probably just using 'apply' when you don't supply scope.

//
//      SCOPE TEST
//
        function whoAmI ()
        {
                trace ( 'I am: ' + this + ' saying: ' + arguments );
        }
        
        whoAmI.apply ( this, ['Hello World'] )
        whoAmI.apply ( null, ['Hola World'] )
        setInterval ( whoAmI, 100, 'Goodbye World' ); 
        
//      I am: _level0 saying: Hello World
//      I am: undefined saying: Hola World
//      I am: undefined saying: Goodbye World



And for as often and inaccurate as they want to fire, I often think the best
solution is to check time change from the last frame to the current with
whatever method. I assumed Intervals only fired once per frame, but
apparently they fire as often and as accurate as they 'can'.

//
//            ONENTERFRAME VS. SETINTERVAL
//

fTime = getTimer()
iTime = getTimer();
var frame = 0;

var id = setInterval ( this, 'onInterval', 0 );
function onInterval () {
                var now = getTimer();
                var change = now - iTime;
                iTime = now;
                trace( 'onInterval: \tf: ' + ( frame ) + '\tc: ' + change );
}

this.onEnterFrame = onFrame;
function onFrame()

{
                var now = getTimer();
                var change = now - fTime;
                fTime = now
                trace( '\nonFrame: \tf: ' + ( frame++ ) + '\tc: ' + change +
newline);
}

//////////////////////////////////////


onFrame:        f: 1    c: 85

onInterval:     f: 2    c: 8
onInterval:     f: 2    c: 8
onInterval:     f: 2    c: 9
onInterval:     f: 2    c: 8
onInterval:     f: 2    c: 7
onInterval:     f: 2    c: 8
onInterval:     f: 2    c: 8
onInterval:     f: 2    c: 9
onInterval:     f: 2    c: 8
onInterval:     f: 2    c: 7
onInterval:     f: 2    c: 28

onFrame:        f: 2    c: 108

onInterval:     f: 3    c: 5
onInterval:     f: 3    c: 7
onInterval:     f: 3    c: 8
onInterval:     f: 3    c: 8
onInterval:     f: 3    c: 8
onInterval:     f: 3    c: 9
onInterval:     f: 3    c: 7
onInterval:     f: 3    c: 8
onInterval:     f: 3    c: 8
onInterval:     f: 3    c: 8
onInterval:     f: 3    c: 9

onFrame:        f: 3    c: 85




_____________________________

Jesse Graupmann
www.jessegraupmann.com  
www.justgooddesign.com/blog/   
_____________________________




-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Muzak
Sent: Wednesday, April 25, 2007 9:59 AM
To: flashcoders@chattyfig.figleaf.com
Subject: Re: [Flashcoders] Clear Set Interval Q:

I was referring to this usage:
clearInterval(setInterval(this, "something", 1000) - 1)

I'd actually have to test this (which I haven't), but it looks to me like
this will prevent 2 intervals to run at any given time.

regards,
Muzak

----- Original Message ----- 
From: "Steven Sacks" <[EMAIL PROTECTED]>
To: <flashcoders@chattyfig.figleaf.com>
Sent: Wednesday, April 25, 2007 6:25 PM
Subject: Re: [Flashcoders] Clear Set Interval Q:


> Danny,
>
> I think you're missing out by not using Intervals.  They're extremely
useful (and efficient) once you get familiar with them.
>
> If you're looking for one that you don't have to keep track of, google
"Kenny Bunch Interval Manager".  It takes care of a lot of 
> interval overhead and puts all intervals in one location (namespace).
>
> To Muzak's point, I'm able to have multiple instances of the same class
each have their own interval running inside them.  Do you 
> mean if you make the interval var static?
>
> -Steven

_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to