Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ActionScript3中的怪异事件监听_Javascript_Actionscript 3_Events - Fatal编程技术网

Javascript ActionScript3中的怪异事件监听

Javascript ActionScript3中的怪异事件监听,javascript,actionscript-3,events,Javascript,Actionscript 3,Events,我在动作脚本中有一个奇怪的怪癖。我需要将索引传递给回调函数 这是我的密码 for (var i:Number = 0; ((i < arrayQueue.length) && uploading); i++) { var lid:ListItemData=ListItemData(arrayQueue[i]); var localI:Number= new Number(i); // to copy? var errorCallback:Functi

我在动作脚本中有一个奇怪的怪癖。我需要将索引传递给回调函数

这是我的密码

for (var i:Number = 0; ((i < arrayQueue.length) && uploading); i++)
{
    var lid:ListItemData=ListItemData(arrayQueue[i]);
    var localI:Number= new Number(i); // to copy?
    var errorCallback:Function = function():void { OnUploadError(localI); };
    var progressCallback:Function = function(e:ProgressEvent):void { lid.progress = e; OnUploadProgress(localI); };
    var completeCallback:Function = function():void { Alert.show('callback'+localI.toString()); OnUploadComplete(localI); }; // localI == arrayQueue.length - 1 (when called)
    Alert.show(localI.toString());  // shows current i as expected
    lid.fileRef.addEventListener(Event.COMPLETE, completeCallback);
    lid.fileRef.addEventListener(ProgressEvent.PROGRESS, progressCallback);
    lid.fileRef.addEventListener(HTTPStatusEvent.HTTP_STATUS, errorCallback);
    lid.fileRef.addEventListener(IOErrorEvent.IO_ERROR, errorCallback);
    lid.fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorCallback);

    lid.fileRef.upload(url, 'File');
}
for(变量i:Number=0;((i

知道如何将索引传递给我的回调吗
.upload
不会阻塞。

可以通过某种委托函数或闭包为回调传递其他参数。然而,这通常被认为是一种不好的做法。您可以使用event
target
属性来根据
FileReference
确定索引

编辑:以下是使用闭包的示例:

function getTimerClosure(ind : int) : Function {
    return function(event : TimerEvent) {
        trace(ind);
    };
}

for (var i = 0; i < 10; i++) {
    var tm : Timer = new Timer(100*i+1, 1);
    tm.addEventListener(TimerEvent.TIMER, getTimerClosure(i));
    tm.start();
}
然而,这是一个糟糕的方法,因为取消对这样一个听众的订阅是非常痛苦的。这反过来可能会导致一些内存泄漏,从而降低应用程序的整体性能。因此,请谨慎使用


底线:如果您知道如何使用闭包,请使用它们-这是一件美妙的事情!如果您不从长远角度考虑应用程序的性能,请使用闭包——这很简单

但如果您不确定闭包,请使用更传统的方法。例如,在您的情况下,您可以创建一个
字典
,将您的
文件引用
对象与适当的索引相匹配。诸如此类:

var frToInd : Dictionary = new Dictionary(false);
// false here wouldn't prevent garbage collection of FileReference objects

for (var i : int = 0; i < 10; i++) {
    // blah-blah stuff with `lib` objects
    frToInd[lib.fileRef] = i;
    // another weird stuff and subscription 
}

function eventListener(event : Event) : void {
    // in the event listener just look up target in the dictionary
    if (frToInd[event.target]) {
        var ind : int = frToInd[event.target];
    } else {
        // Shouldn't happen since all FileReferences should be in 
        // the Dictionary. But if this happens - it's an error.
    }
}
var frToInd:Dictionary=newdictionary(false);
//此处为false不会阻止FileReference对象的垃圾收集
对于(变量i:int=0;i<10;i++){
//用'lib'对象之类的废话
frToInd[lib.fileRef]=i;
//另一个奇怪的东西和订阅
}
函数eventListener(事件:事件):void{
//在事件侦听器中,只需在字典中查找目标
if(frToInd[event.target]){
var ind:int=frToInd[event.target];
}否则{
//不应该发生,因为所有文件引用都应该在
//但如果发生这种情况,那就是一个错误。
}
}
--快乐编码

我在动作脚本中有一个奇怪的怪癖

这不是怪癖,而是可变范围。您应该阅读这篇文章:

你真的不应该使用匿名,它只会让一切变得更加混乱。实际上,您正在制作同一对象的多个副本

如果arrayQueue在范围内,则可以使用以下代码获取索引:

GetArrayIndex(e.currentTarget);

function GetArrayIndex(object:Object):Number
{
    for(var i:Number = 0; 0 < arrayQueue.length; i++)
    {
        if(object === arrayQueue[i])
            return i;
    }
}
GetArrayIndex(如currentTarget);
函数GetArrayIndex(对象:对象):编号
{
对于(变量i:Number=0;0

你应该考虑使用索引来做索引。< /P> <代码>目标< /代码>对我无效,因为<代码>目标< /代码>只会得到<>代码>文件引用< /代码>,这不是我所需要的。但是< <代码>文件> <代码> s对于不同的代码> Le> < /C> > s也不同。至少您可以将它们存储在数组中,并在回调中找到必要的一个。但是使用闭包是个坏主意。为什么在这里使用闭包是个坏主意?我觉得你的例子非常好。@Daniel,@Chetan:我添加了一些解释、代理创建示例和问题的正确解决方案。

GetArrayIndex(e.currentTarget);

function GetArrayIndex(object:Object):Number
{
    for(var i:Number = 0; 0 < arrayQueue.length; i++)
    {
        if(object === arrayQueue[i])
            return i;
    }
}