Actionscript 3 forEach中的AS3 dispatchEvent
我已经填充了一个数组 我需要获取该数组中的每个项,对该项执行一些计算,将结果推送到数组中,然后移动到数组中的下一项,依此类推 这些计算是在一个单独的类中进行的。然后,当计算完成时,我发送一个事件,并监听类完成 我正在使用forEach,但我需要暂停forEach函数并等待dispatchEvent侦听器,然后再继续,但我似乎无法让它工作。 跟踪似乎表明forEach只是在数组中运行,每次都重置计算类 这是我的代码的jist。我从服务器上的sql表填充数组,然后启动forEach 有人能提出解决方案吗Actionscript 3 forEach中的AS3 dispatchEvent,actionscript-3,foreach,wait,dispatchevent,Actionscript 3,Foreach,Wait,Dispatchevent,我已经填充了一个数组 我需要获取该数组中的每个项,对该项执行一些计算,将结果推送到数组中,然后移动到数组中的下一项,依此类推 这些计算是在一个单独的类中进行的。然后,当计算完成时,我发送一个事件,并监听类完成 我正在使用forEach,但我需要暂停forEach函数并等待dispatchEvent侦听器,然后再继续,但我似乎无法让它工作。 跟踪似乎表明forEach只是在数组中运行,每次都重置计算类 这是我的代码的jist。我从服务器上的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);
}
函数handleLoadSuccessful(evt:事件):无效
{
evt.target.dataFormat=URLLoaderDataFormat.TEXT;
已更正变量:字符串=evt.target.data;
校正=校正切片(1,校正长度-1);
var结果:URLVariables=新的URLVariables(已更正);
如果(result.errorcode==“0”)
{
对于(变量i:Number=0;i
如果您需要暂停脚本执行以等待函数完成,则调度事件不是您想要的方式。您要做的是调用的函数返回您正在等待的值,并且根本不使用事件。如果我知道您在allocationbible中所做的事情,可能会有更大的帮助。您不能为每个
循环暂停,这在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();
}
}
element.code是一个30位的产品代码,不同的子字符串等于不同的参数。根据这些参数,我计算原材料的产量,分配圣经计算的就是这种原材料。除了使用forEach,还有其他路线吗?我使用dispatchEvent引用了几个sql表,以获取计算分配所需的信息。