views:

332

answers:

2

On occasion, I have wanted to push a closure onto ActionScript's event stack so that it will be executed after the current event handler. Right now I use setTimeout(closure, 0). Is there a more direct way of doing this?

A: 

Take a look at capture and bubbling phases of the as3 events. I found this nice chapter that explains clearly the proccess: http://books.google.com/books?id=yFNZGjqJe9IC&lpg=PA250&ots=oPB9HXIby7&dq=flash%20event%20bubbling%20phase&pg=PA250#v=onepage&q=&f=false

And also check the EventDispatcher class documentation that explain the use of this different phases.

ruyadorno
I'm sorry… I don't quite understand how this is related.
David Wolever
+3  A: 

setTimeout(closure, 0) is creating a new event stack. I don't understand your objective if this solution isn't working for you. What is the goal you're trying to accomplish?

Flex has ENTER_FRAME events, Timer, callLater, setTimeout, setInterval, all which delay calls and create new execution stacks.

Are you trying to inject code into the current stack? If so, you might need to look at something like this: http://en.wikipedia.org/wiki/Active_object. The idea being that you push functions (closures) into an array, and the active object controller pulls the next one off the list when the previous one has run to completion. That's the simplest case. You can write a more complicated one that will have priority stacks like high, medium, low, with your own schedule management system. (e.g., low get promoted after waiting too long).

But hey! The devil is in the details. What's the goal?

Glenn
Thanks – I'll look into the functions you mentioned.And the goal is basically to make my intent ("I want to push a closure onto the execution stack so it will be executed some indeterminate amount of time after the current event handler finishes") clear in the code, and `setTimeout(..., 0)` doesn't feel like it makes things very clear (unless, of course, you know something about the inner workings of setTimeout).
David Wolever
Ok, so callLater isn't quite what I'm looking for… But it might be the best I'll get, thanks.
David Wolever
Don't hate setTimeout. It's been around since the early days of JavaScript. But if you need it for arbitrary delays, there's probably something wrong with your design. I've never found callLater all the reliable of a "wait". At least with setTimout, you know how long you're waiting, even if it's a hack.
Glenn