我已经填充了一个数组。
我需要获取数组中的每一项,对该项执行一些计算,将结果推送到数组中,然后转移到数组中的下一项,以此类推。
计算是在一个单独的类中进行的。然后,当计算完成时,我会分派一个事件并侦听类是否完成。
,我正在使用forEach,但我需要暂停forEach函数,在继续之前等待dispatchEvent侦听器,但是我似乎无法让它工作。
跟踪似乎表明forEach只是在数组中运行,每次都重置计算类。
这是我的代码。我从服务器上的sql表填充数组,然后启动forEach。
有谁能提出解决办法吗?
function handleLoadSuccessful(evt:Event):void
{
evt.target.dataFormat = URLLoaderDataFormat.TEXT;
var corrected:String = evt.target.data;
corrected = corrected.slice(1,corrected.length-1);
var result:URLVariables = new URLVariables(corrected);
if (result.errorcode=="0")
{
for (var i:Number=0; i < result.n; i++)
{
liveOrderArray.push(
{
code:result["ItemCode"+i],
qty:Number(result["LineQuantity"+i]) - Number(result["DespatchReceiptQuantity"+i])
})
}
liveOrderArray.forEach(allocate);
} else {
trace("ERROR IN RUNNING QUERY");
}
}
function allocate(element:*, index:int, arr:Array):void {
trace("code: " + element.code + " qty:" + element.qty );
allocationbible.profileCode = element.code.substring(0,1);
allocationbible.finishThk = Number(element.code.substring(1,3));
allocationbible.longEdgeCode = element.code.substring(3,4);
allocationbible.backingDetailCode = element.code.substring(4,5);
allocationbible.coreboardCode = element.code.substring(5,6);
allocationBible = new allocationbible;
allocationBible.addEventListener("allocated", updateAllocationQty, false, 0, true);
trace("*************************************");
}
function updateAllocationQty (evt:Event):void {
//add result to array
trace(allocationbible.coreboardLongCode);
}发布于 2012-04-19 16:40:40
如果需要停止脚本的执行以等待函数完成,那么调度事件并不是您想要的方式。您要做的是调用函数,返回正在等待的值,并且根本不使用事件。
如果我知道你在分配圣经里做了什么,也许会有更多帮助
发布于 2012-04-19 22:25:21
不能暂停for..each循环,这在AS3中是不可能的。所以你需要重写你的代码。
UPD:上次误解了你的问题。若要在计算完成后再进行进一步处理,可以开始处理分配事件处理程序中的下一个元素。就像这样:
var currentItemIndex:int;
function startProcessing():void {
// population of the array
// ...
allocationBible = new allocationbible();
allocationBible.addEventListener("allocated", onAllocated);
currentItemIndex = 0;
allocate();
}
function allocate():void {
var element:* = liveOrderArray[currentItemIndex];
// configure allocationBible
allocationBible.process(element);
}
function onAllocated(e:Event):void {
trace("Allocated: " + allocationbible.coreboardLongCode);
// allocate the next item
currentItemIndex++;
if (currentItemIndex >= liveOrderArray.length) {
allocationBible.removeEventListener("allocated", onAllocated);
} else {
allocate();
}
}https://stackoverflow.com/questions/10232291
复制相似问题