Actionscript 3 如何仅在已分配参数变量时运行函数?

Actionscript 3 如何仅在已分配参数变量时运行函数?,actionscript-3,event-handling,wait,custom-events,Actionscript 3,Event Handling,Wait,Custom Events,我需要一种方法来等待运行parseCSV命令,直到readFile事件更新了importData的内容。我已经了解了一些关于定制事件调度器的事情,但还不能完全理解如何在我的情况下使用它们 private var importData : String; public function importFile(event:MouseEvent):void { var data:String = chooseFile(); parseCSV(importDat

我需要一种方法来等待运行parseCSV命令,直到readFile事件更新了importData的内容。我已经了解了一些关于定制事件调度器的事情,但还不能完全理解如何在我的情况下使用它们

private var importData : String;

    public function importFile(event:MouseEvent):void {
        var data:String = chooseFile();
        parseCSV(importData);
    }

    public function chooseFile ():String {
        var filetype:FileFilter = new FileFilter("CSV Files(*.csv)","*.csv");
        var file:File = File.userDirectory;
        file.browseForOpen("Select CSV file to import", [filetype]);
        file.addEventListener(Event.SELECT, readFile);
        return importData;
    }

public function readFile (event:Event):void {
        var filestream:FileStream = new FileStream();
        filestream.open(event.target as File, FileMode.READ);
        importData = filestream.readUTFBytes(filestream.bytesAvailable);
        filestream.close();
    }

您需要添加一些回调或添加一些事件侦听器。我更喜欢回拨:

function importFile(...) {
     choseFile(function(file:File) {
         readFile(file, parseCSV);
     });
}

function choseFile(callback:Function) {
    ...
    file.addEventListener(Event.SELECT, function(event:Event) {
        callback(File(event.target));
    });
}

function readFile(file:File, callback:Function) {
    var data = ... read data from file ....;
    callback(data);
}

您需要添加一些回调或添加一些事件侦听器。我更喜欢回拨:

function importFile(...) {
     choseFile(function(file:File) {
         readFile(file, parseCSV);
     });
}

function choseFile(callback:Function) {
    ...
    file.addEventListener(Event.SELECT, function(event:Event) {
        callback(File(event.target));
    });
}

function readFile(file:File, callback:Function) {
    var data = ... read data from file ....;
    callback(data);
}

在readFile函数中添加一行怎么样

public function readFile (event:Event):void {
    var filestream:FileStream = new FileStream();
    filestream.open(event.target as File, FileMode.READ);
    importData = filestream.readUTFBytes(filestream.bytesAvailable);
    parseCSV(importData);
    filestream.close();
}
设置importData后,将立即执行该命令

如果希望发送自定义事件路由,则需要发送自己的自定义事件。每个事件都有一个类型参数,它只是一个用来标识它的字符串。例如Event.CHANGE与使用CHANGE相同

static public const CUSTOM = "myCustomEvent";

public function someConstructor():void {
    addEventListener(CUSTOM, onCustomEvent);
}

public function testDispatch():void{
    dispatchEvent(new Event(CUSTOM));
}

private function onCustomEvent(e:Event):void{
    trace("custom event Dispatched");
}
所以你可以试试这样的

public function importFile(event:MouseEvent):void {
        addEventListener(CUSTOM, onImport);
        var data:String = chooseFile();
    }

private function onImport(e:Event):void {
    parseCSV(importData);
}

public function readFile (event:Event):void {
    var filestream:FileStream = new FileStream();
    filestream.open(event.target as File, FileMode.READ);
    importData = filestream.readUTFBytes(filestream.bytesAvailable);
    dispatchEvent(new Event(CUSTOM));
    filestream.close();
}

在readFile函数中添加一行怎么样

public function readFile (event:Event):void {
    var filestream:FileStream = new FileStream();
    filestream.open(event.target as File, FileMode.READ);
    importData = filestream.readUTFBytes(filestream.bytesAvailable);
    parseCSV(importData);
    filestream.close();
}
设置importData后,将立即执行该命令

如果希望发送自定义事件路由,则需要发送自己的自定义事件。每个事件都有一个类型参数,它只是一个用来标识它的字符串。例如Event.CHANGE与使用CHANGE相同

static public const CUSTOM = "myCustomEvent";

public function someConstructor():void {
    addEventListener(CUSTOM, onCustomEvent);
}

public function testDispatch():void{
    dispatchEvent(new Event(CUSTOM));
}

private function onCustomEvent(e:Event):void{
    trace("custom event Dispatched");
}
所以你可以试试这样的

public function importFile(event:MouseEvent):void {
        addEventListener(CUSTOM, onImport);
        var data:String = chooseFile();
    }

private function onImport(e:Event):void {
    parseCSV(importData);
}

public function readFile (event:Event):void {
    var filestream:FileStream = new FileStream();
    filestream.open(event.target as File, FileMode.READ);
    importData = filestream.readUTFBytes(filestream.bytesAvailable);
    dispatchEvent(new Event(CUSTOM));
    filestream.close();
}

@托多罗斯。。我不想从readfile调用它的原因是我希望importFile函数控制函数流,包括parsecsv.@Todorus.之后的函数流。。我不想从readfile调用它的原因是我希望importFile函数控制函数流,包括parsecsv之后的函数流。