Dealing with events while loop is running

Well I have been scratching my head over this quite some time now.
I am calling a test function from a loop, and would like to throw an exception when cancel button is clicked, and break operation.
However while executing the loop everything else will halt, user input wont be captured events wont fire and soon… so pretty much I cannot find a way do do this.

import flash.events.MouseEvent;
cancel_btn.addEventListener(MouseEvent.CLICK,stopLoop);
test_btn.addEventListener(MouseEvent.CLICK,test);
function stopLoop (event) {
    throw new Error ("cancel");
}

function test (event:MouseEvent) {
//endless loop to cancel
    for (;;) {
    }
}

Only way I found out is instead of using loop, is to recursively call function with timeout like this

import flash.events.MouseEvent;
cancel_btn.addEventListener(MouseEvent.CLICK,stopLoop);
test_btn.addEventListener(MouseEvent.CLICK,loop);
var repeatCounter:Number=0;
function stopLoop (event) {
    try{
        throw new Error ("cancelled after " + repeatCounter+" repetititons");
    }catch(e){
        trace(e.message)
    }
}

function loop (event:MouseEvent) {
    test(repeatCounter);
}

function test (counter) {
    repeatCounter=counter;
    setTimeout(test,1,counter+1);
}

This kinda works, but

  1. adds latency from timeout and function call
  2. makes things asynchronous
  3. timeout error won’t be thrown, so either I have to put custom timeout, or this can goon forever if not canceled (just put the timeout time to 0, and FlashPlayer will hijack your machine until powerdown…)

any other ideas?