如何仅在分配参数变量时才运行函数?
我需要一种方法来等待运行 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();
}
I need a way to wait running the parseCSV command until the readFile event has updated the content of importData. I have seen a few things about custom event dispatchers but cannot quite figure out how to use them in my situation.
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要添加一些回调或添加一些事件侦听器。我更喜欢回调:
You'll either need to add some callbacks or add some event listeners. I prefer callbacks:
只在 readFile 函数中添加这一行怎么样?
一旦设置了 importData,该命令就会被执行。
如果您希望自定义事件路由,则需要调度您自己的自定义事件。每个事件都有一个类型参数,它只是一个用于标识它的字符串。例如,Event.CHANGE 与使用“change”相同。
所以你可以尝试这样的事情。
What about just adding the line in the readFile function?
The command will be executed as soon as importData is set.
If you wish to the custom events route, you need to dispatch your own custom Event. Each Event has a type parameter which is just a string to identify it with. For example Event.CHANGE is the same as using "change".
So you could try something like this.