Use CallLater()方法
来源:互联网 发布:用php开发简单的商城 编辑:程序博客网 时间:2024/05/19 01:33
The callLater() method queues an operation to be performed for the next screen refresh(下一帧), rather than in the current update. Without the callLater() method, you might try to access a property of a component that is not yet available.
Have you ever tried to do something like this:
var someText:Text = new Text();
someText.text="testing";
addChild(someText);
trace(someText.measuredWidth);
var someText:Text = new Text();
someText.text="testing";
addChild(someText);
callLater(getWidth);
public function getWidth():void{
trace(someText.measuredWidth);
}
callLater vs. validateNow
For years, many Flex developers have known the secret to fixing many a UI problem was using either callLater() or validateNow() methods. Much to my surprise, some of these developers do not understand WHY these fix problems, what the difference is, and which method to use.
So why do we need these functions? Well, in short, the flex framework doesn't do anything in the order you tell it and it doesn't do what you tell it to do when you tell it to. If you set a text's width and a text's style, they may not be applied in that order. And they will probably not be applied until a bunch of other stuff has happened either. When a programmer thinks something is done synchronously (one after the other) when they're actually being done according to the component when it "validates". It is important to note that this is not an issue with ActionScript 3, so much as an issue with the Flex Framework.
callLater vs. validateNow
callLater accepts two parameters. A function and some arguments for the function. This "function" gets queued to be called again at a later frame, hopefully after everything else has finished processing..
validateNow accepts no parameters and basically tells your component to just do its processing RIGHT NOW! So the difference is, instead of waiting and executing, like callLater, it just executes right now. From a programmer's perspective, this is a lot easier to do, however, it is not faster. It forces Flex to reprocess a bunch of stuff, whereas callLater just executes the code you wanted to execute anyway at a later time. If you want to use validateNow, consider that there are some more specific functions such as validateSize, validateProperties, and validateDisplayList. All 3 of these functions are called by validateNow.
Code vs. Code
The situation is that you want the width of some rendered text. After setting the text, you can not get the measuredwidth property because it will still be 0 until the component has validated.
CallLater Solution
var someText:Text = new Text();
someText.text="testing";
addChild(someText);
trace("before: " + someText.measuredWidth);
callLater(getWidth);
public function getWidth():void{
trace("after: " + someText.measuredWidth);
}
validateNow Solution
var someText:Text = new Text();
someText.text="testing";
addChild(someText);
trace("before: " + someText.measuredWidth);
someText.validateNow(); //you should actually use validateSize() if you just want the measured width
trace("after: " + someText.measuredWidth);
Hopefully this makes sense. In my opinion, it is generally better to use callLater as it won't cause any unneeded computation.
转载:http://leetechlife.blogbus.com/logs/82845226.html
- Use CallLater()方法
- callLater方法
- callLater解析
- Flex之旅:第二部分:容器与布局(4)--- callLater()方法的使用
- Using the callLater() method
- Flex中的callLater和validateNow
- app.use()方法
- Flash & Flex组件优化的杀手锏callLater
- Flex\flash中的callLater()函数作用
- Flex\flash中的callLater()函数作用
- performance问题排查 - USE方法
- USE
- use
- use
- use
- USE
- 动态调用方法Use the NSInvocation class
- use of undeclared identifier 'xxxxxxx方法名'
- Objective-C语法之KVO的使用
- notepad++列编辑
- MVC 密码加密(哈希加密)
- VisualStudio2008 快捷键大全
- c# 压缩解压代码
- Use CallLater()方法
- ASPX页面缓存
- standby_archive_dest和log_archive_dest_n区别
- Runtime.getRuntime().exec 导致的内存溢出
- ubuntu安装apache2
- 面向过程的分析方法
- 快速排序算法
- 怎样查出连续数字中缺失的数字
- listview dialog出错