ActionScript 3:如何使用匿名函数删除 EventListener

发布于 2024-12-29 12:16:19 字数 1075 浏览 1 评论 0原文

我写的代码如下。
问题是我无法删除 Event.COMPLETE 事件侦听器,并且当我调用 loadData 函数两次或更多次时,它会工作 2 次或更多次。抱歉我的英语不好,解释也更糟糕,但我今天需要修复它,但我不知道该怎么做。
我认为代码非常明显。请帮忙!

var ldr:URLLoader = new URLLoader();

function loadData(text_place, scrollbar, fileURL:String):void {
    text_place.wordWrap = true;
    var f:TextFormat = new TextFormat();
    f.align = TextFormatAlign.RIGHT;
    text_place.setTextFormat(f);
    ldr.dataFormat = URLLoaderDataFormat.TEXT;
    ldr.load(new URLRequest(fileURL));
    ldr.addEventListener(Event.COMPLETE, function ldr_complete(evt:Event){ 
        initText(text_place, ldr.data, scrollbar);
    });
    ldr.addEventListener(IOErrorEvent.IO_ERROR, loadError);
}

function initText(text_place:TLFTextField, fileContent, scrollbar):void {
    ldr.removeEventListener(IOErrorEvent.IO_ERROR, loadError);
    text_place.htmlText = "";
    text_place.tlfMarkup = fileContent;
    scrollbar.update();
    trace("Data loaded");
}

function loadError(e:IOErrorEvent):void {
    trace("Error loading an external file.");
}

I have written code as follows.
Problem is that I can't remove Event.COMPLETE event listener and when I call the loadData function twice or more, it works 2 times or more. Sorry for my bad english and worse explanation but I need to fix it today and I don't know what to do.
I think the code is pretty obvious. please help!

var ldr:URLLoader = new URLLoader();

function loadData(text_place, scrollbar, fileURL:String):void {
    text_place.wordWrap = true;
    var f:TextFormat = new TextFormat();
    f.align = TextFormatAlign.RIGHT;
    text_place.setTextFormat(f);
    ldr.dataFormat = URLLoaderDataFormat.TEXT;
    ldr.load(new URLRequest(fileURL));
    ldr.addEventListener(Event.COMPLETE, function ldr_complete(evt:Event){ 
        initText(text_place, ldr.data, scrollbar);
    });
    ldr.addEventListener(IOErrorEvent.IO_ERROR, loadError);
}

function initText(text_place:TLFTextField, fileContent, scrollbar):void {
    ldr.removeEventListener(IOErrorEvent.IO_ERROR, loadError);
    text_place.htmlText = "";
    text_place.tlfMarkup = fileContent;
    scrollbar.update();
    trace("Data loaded");
}

function loadError(e:IOErrorEvent):void {
    trace("Error loading an external file.");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

懵少女 2025-01-05 12:16:19

只需避免编写函数封装并扩展完整函数传递的参数的范围,以便它可以访问它们。

var ldr:URLLoader = new URLLoader();
var text_place:TextField;
var scrollbar:Object; //or whatever it is

function loadData(text_place, scrollbar, fileURL:String):void
{
    var f:TextFormat = new TextFormat();
    f.align = TextFormatAlign.RIGHT;

    text_place.wordWrap = true;
    text_place.setTextFormat(f);

    scrollbar = scrollbar;

    ldr.dataFormat = URLLoaderDataFormat.TEXT;
    ldr.load(new URLRequest(fileURL));

    ldr.addEventListener(IOErrorEvent.IO_ERROR, loadError);
    ldr.addEventListener(Event.COMPLETE, loadComplete);
}

function initText(text_place:TLFTextField, fileContent, scrollbar):void
{
    removeLoaderEventListeners();

    text_place.htmlText = "";
    text_place.tlfMarkup = fileContent;

    scrollbar.update();

    trace("Data loaded");
}

function loadError(e:IOErrorEvent):void
{
    removeLoaderEventListeners();

    trace("Error loading an external file.");
}

function loadComplete(evt:Event):void
{
    removeLoaderEventListeners();

    initText(text_place, ldr.data, scrollbar);
}

function removeLoaderEventListeners():void
{ 
    ldr.removeEventListener(IOErrorEvent.IO_ERROR, loadError);
    ldr.removeEventListener(Event.COMPLETE, loadComplete);
}

just avoid writing function enclosures and extend the scope of the complete function's passed arguments so it can access them.

var ldr:URLLoader = new URLLoader();
var text_place:TextField;
var scrollbar:Object; //or whatever it is

function loadData(text_place, scrollbar, fileURL:String):void
{
    var f:TextFormat = new TextFormat();
    f.align = TextFormatAlign.RIGHT;

    text_place.wordWrap = true;
    text_place.setTextFormat(f);

    scrollbar = scrollbar;

    ldr.dataFormat = URLLoaderDataFormat.TEXT;
    ldr.load(new URLRequest(fileURL));

    ldr.addEventListener(IOErrorEvent.IO_ERROR, loadError);
    ldr.addEventListener(Event.COMPLETE, loadComplete);
}

function initText(text_place:TLFTextField, fileContent, scrollbar):void
{
    removeLoaderEventListeners();

    text_place.htmlText = "";
    text_place.tlfMarkup = fileContent;

    scrollbar.update();

    trace("Data loaded");
}

function loadError(e:IOErrorEvent):void
{
    removeLoaderEventListeners();

    trace("Error loading an external file.");
}

function loadComplete(evt:Event):void
{
    removeLoaderEventListeners();

    initText(text_place, ldr.data, scrollbar);
}

function removeLoaderEventListeners():void
{ 
    ldr.removeEventListener(IOErrorEvent.IO_ERROR, loadError);
    ldr.removeEventListener(Event.COMPLETE, loadComplete);
}
孤凫 2025-01-05 12:16:19

如果您想在事件触发后停止侦听事件,则可以取消注册匿名侦听器本身:

ldr.addEventListener(Event.COMPLETE, function(event:Event):void
{
     event.target.removeEventListener(event.type, arguments.callee);
     // ... do whatever you need to do here
});

但如果您还想在事件完成时停止侦听来自同一调度程序的其他事件,例如您的 IOErrorEvent.IO_ERROR 监听器,您仍然需要引用该监听器才能将其删除。

if you want to stop listening for an event after it triggered, you can unregister the anonymous listener in itself:

ldr.addEventListener(Event.COMPLETE, function(event:Event):void
{
     event.target.removeEventListener(event.type, arguments.callee);
     // ... do whatever you need to do here
});

But if you also want to stop listening for other events from the same dispatcher when it completes, such as your IOErrorEvent.IO_ERROR listener, you'd still need a reference to that listener to remove it.

有一个更简单的方法。不要删除事件侦听器,而是关闭加载程序。

ldr.close();

根据文档:

关闭正在进行的加载操作。任何正在进行的加载操作
立即终止。如果当前没有正在传输 URL,则
抛出无效流错误。

There is a simpler way. Instead of removing event listeners, close the loader.

ldr.close();

Per the documentation:

Closes the load operation in progress. Any load operation in progress
is immediately terminated. If no URL is currently being streamed, an
invalid stream error is thrown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文