代码之家  ›  专栏  ›  技术社区  ›  Structure

在AS3中,URLLoader.close()是否存在;如果没有加载任何内容会导致问题?

  •  4
  • Structure  · 技术社区  · 16 年前

    URLLoader.close();

    我有一个简单的应用程序,用户在文本框中输入信息,然后将信息发送到PHP脚本并返回输出。收到输出后,您可以单击一个新按钮来重置应用程序。错误在于单击该按钮后执行的函数。

    如果你点击这个按钮来重置应用程序,它会重置所有变量,等等,但是看起来我的 addEventListener 方法不执行。

    以下是重置功能:

    //Reset function if the Reset button is pressed
    function clearApplication(e:MouseEvent):void {
        receivedData="";
        data1TextBox.text="";
        data2TextBox.text="";
        resetButton.visible=false;
        resetButton.removeEventListener(MouseEvent.CLICK, clearApplication, false);
        goButton.visible=true;
        goButton.addEventListener(MouseEvent.CLICK, getData, false, 0, true);
        myLoader.close();
    }
    

    goButton 不再有效。

    如果 戈布顿 单击时,应执行以下操作:

    function getData(e:MouseEvent):void {
        if (data1TextBox.text!=""&&data2TextBox.text!="") {
            goButton.removeEventListener(MouseEvent.CLICK, getData, false);
            goButton.visible=false;
    
            postVars = new URLVariables();
            postVars.data1=data1TextBox.text;
            postVars.data2=data2TextBox.text;
    
            myRequest=new URLRequest("URL");
            myRequest.method=URLRequestMethod.POST;
            myRequest.data=postVars;
    
            myLoader.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
            myLoader.load(myRequest);
        }
    }
    

    function onComplete(e:Event):void {
        receivedData=e.target.data;
    
        outputTextBox.text=receivedData;
    
        resetButton.visible=true;
        resetButton.addEventListener(MouseEvent.CLICK, clearApplication, false, 0, true);
    }
    

    请注意,没有在函数中声明的任何变量都是全局声明的。

    myLoader.close(); 从 clearApplication 然后一切顺利。我的goButton没有问题,如果所有文本字段都已填充,则会按预期执行。

    我知道这是没有必要的,如果没有目前正在加载,但它是存在的,因为我永远不知道它是否会挂起,需要取消。为什么会呢

    编辑:抱歉,在简化示例代码时犯了一些错误。

    1 回复  |  直到 16 年前
        1
  •  5
  •   Amarghosh    16 年前

    做 URLLoader.close()

    是的,是的。从 livedocs page for URLLoader.close()

    任何正在进行的加载操作都将立即终止。如果当前没有正在流式传输的URL,则会引发无效流错误。

    debug version of Flash player

    如果你把电话转到 close() 在try block和catch错误中,可以看到发生了什么。

    try 
    {
      myLoader.close();
    }
    catch(e:Error) 
    {
      trace("An error occurred " + e.toString());
    }