There is no such function in Flash as executing a function asynchronously, you have to do it yourself if you do not want to use Workers (for example, Vesper). Workers give you a separate process. Otherwise, you will have to break your calculation into pieces. Here's how you do it:
Tracing images is a very difficult operation. This is not so, but just for illustration. This simple for-loop runs on a frame and causes a lower frame rate, since they are all calculated before the frame is actually displayed.
for(var i:int = 0; i < 1000; i ++) { trace(i);
So, you need to break the calculation into pieces and break it in order to be able to run the calculation over time.
To do this, you need to create a function that simply takes part of the loop every time:
calculatePart(0, 1000, 20); function calculatePart(startIndex:int, endIndex:int, amountPerRun:int) { for(var i:int = startIndex; (i < startIndex + amountPerRun) || (i < endIndex); i ++) { trace(i);
This is actually the same function as the simple for loop in the first code, and it also prints 1000 traces. He is ready to work in parts, but this is not async yet. Now we can easily change the function, so the function works over time. For this, I use setTimeout . You can also use the ENTER_FRAME event-listener or Timer class for this, but for this example I will try to clear it.
calculatePart(0, 1000, 20, 100); function calculatePart(startIndex:int, endIndex:int, amountPerRun:int, timeBeforeNextRun:Number) { for(var i:int = startIndex; (i < startIndex + amountPerRun) && (i < endIndex); i ++) { trace(i);
As you can see, I added the timeBeforeNextRun parameter. If you run this example, you will see that it takes 100 milliseconds to output 20 traces.
If you set it very low, the calculation will be delivered very quickly, but you wonβt be able to get extra speed just trying to do more in less time. You have to play with time and quantity variables, you can check which one actually gives the best performance (or less lag).
Hope this helps.
If you want to use a more reasonable time calculation, I found this utility class very useful, which measures how much time the calculation actually took and changed the time.