all groups > flash actionscript > august 2005 >
You're in the

flash actionscript

group:

setInterval() ... why skipping interval id's?


setInterval() ... why skipping interval id's? sarova
8/15/2005 7:08:31 PM
flash actionscript: It is with a heavy heart I must admit my inability to understand what's going
on here -

The following code causes the intervalID's to skip 8, or 10, or 11 increments
when a row in the datagrid is clicked.
Why? And how can I prevent this? - my movie relies on predictably
incrementing interval timers:

If you would like to try this experiment for yourself
1. Open a new blank movie
2. Drag a dataGrid object onto the stage in frame 1 and call it "theDataGrid"
3. Drag any movie object onto the stage in frame 1 and call it "Movie"
4. Copy the following AS into frame 1 and test by clicking on the Movie and
any row in theDataGrid a couple of times ...



// set up a data grid with one column which is a list of five names - you will
need to drag a dataGrid object to your stage and name it "theDataGrid"

var Name_array:Array = new Array("Belinda", "Gina", "Kathy", "Charlotte",
"Jane");
myDP = new Array();
for(i=0; i<5; i++) {
myDP.addItem({Names:Name_array[i]});
}
theDataGrid.dataProvider = myDP;

// set up listener for when grid row is clicked
var userListener = new Object();
userListener.change = function(event) {
trace("You clicked on "+event.target.selectedItem.Names);
};

theDataGrid.addEventListener("change", userListener);

// now set up a movie clip that clears and sets a new interval the executes a
function called "test" 3 times - you will have to create this movie object
(anything will do, say, a rectangle) and name it "Movie"
var cc = 1;
Movie.onPress = function() {
clearInterval(ID);
ID = setInterval(test,200);
trace("Setting New Interval ID # "+ID);
trace("It will now execute 3 times ...");
};

function test() {
trace("Interval "+ID+" loop "+cc);
cc++;
if(cc>3) {
clearInterval(ID);
cc = 1;
}
};
stop();
Re: setInterval() ... why skipping interval id's? Rothrock
8/15/2005 10:40:54 PM
Jeckyl, do you know what magic is used to assign the interval ids? It seems odd
that they wouldn't be assigned in numerical order. My guess is that some of the
component or listeners are also using setInterval and thereby using up some
numbers? Just wondering.

And I agree, you never can count on them being what you expect. So if you want
to keep track of them, then you better track them yourself, cause Flash won't
do it for you. (Really good advice about so many things in Flash.)
Re: setInterval() ... why skipping interval id's? Jeckyl
8/16/2005 7:01:38 AM
[quoted text, click to view]

NOOO!!!

You CANNOT and SHOULD not predict interval timers.

Do not write code that way .. it will fail.

There is no guarantees what id numbers will be returned for an interval.

The problem is not with the id's but with your design relying on something
that just isn't so. It would be like building a house so you could see the
sunrise every morning on the assumption that the sun rises in the west.

If you want, you could (say) store the returned interval numbers in an
array, and then you can access them by a known array index (eg the third
item in the array is your third interval, regardless of what id it is).
--
Jeckyl

Re: setInterval() ... why skipping interval id's? sarova
8/20/2005 12:00:00 AM
Thank you for both replies which are good advice. Yes it turns out they are
unpredictable if there are other event listeners in the scene (which makes
sense Rothrock that they would need to setIntervals too). It also turns out
that the problem I was having that I thought was being caused by spurious
intervalIDs had nothing to do with this, and therefore, contrary to my
assertion above I do not need to rely on them (which should relieve some of
your worry Jeckyl). My problem had to do with multiple calls to a function
that used setInterval. This caused multiple setInterval timers to concurrently
be calling the same function (the one named in the setInterval command) making
its context incoherent. The solution was to write as many separate copies of
the re-entered function as would be needed to execute concurrently. I did not
know another way to handle reentrant functions.

Thanks to both for helping me find the problem, even if the solution is
inelegant.
AddThis Social Bookmark Button