Actionscript SetInterval problems

Subscribe to Actionscript SetInterval problems 6 posts, 3 voices

 
avatar for Balidani Balidani 133 posts

Hi, I am trying to call a function every second, but after the first second it starts massively calling it over and over, without waiting one second. Can anyone please help? this is what i have:

on the first frame:
function doIt() {
    trace("gotcha");
    clearInterval(intervalID);
}

and on the second frame:

onEnterFrame = function(){
    moveIt();
    intervalID = setInterval(doIt,1000);
}

I would really appreciate your help :)

 
avatar for Lysis Lysis 320 posts
Put
stop();

on the second frame.

 
avatar for Balidani Balidani 133 posts

It doenst seem to help :(

 
avatar for Phantasmagoria Phantasmagoria 277 posts

The code on the second frame (of the timeline) is calling setInterval every frame (every moment that the game is running) while on the second frame of the timeline. Presumably, the game is sitting on this frame for more than one frame. (This sure sounds confusing, since “frame” means two different things here… great job, Adobe/Macromedia!) Thus, you’re calling setInterval dozens of times before the interval triggers even once.

 
avatar for Lysis Lysis 320 posts

Yeah, he’s right; I’m an idiot – didn’t see you had the interval setup in an onEnterFrame function.

You only need to set the interval once, ie move it outside the onEnterFrame function.

I was thinking your movie was looping – and you don’t want to do that either, so make sure you stop it somewhere.

 
avatar for Balidani Balidani 133 posts

Thanks guys, it makes sense now. I’ll try if it works later, but i’m almost certain it will.