When flex is used for instant messaging, the latest message received should be at the bottom, but the scroll bar of textArea is at the top by default, which is inconvenient to view the latest message.
You can use TextArea. verticalScrollPosition = TextArea. maxVerticalScrollPosition; To locate at the bottom, but in actual implementation, it is always impossible to scroll to the last, but to the second to the last.
Someone may encounter the same problem as me in ActionScript3: how to locate the vertical scroll bar to the bottom
Later, I asked in the group because the components have not been refreshed even though the data source has been updated. There are two solutions:
1. validateNow (): Call this function before verticalScrollPosition is set to force refresh the component. (Although this method is called by a component, it will refresh the entire UI, resulting in excessive CPU usage, which is not recommended. In fact, the validateNow () of UIComponent also uses timer and enterframe internally, so the following method is recommended)
2. Add a time delay. It is hard to say how long the delay is to be added. You cannot feel the delay and ensure that the component has been refreshed. Therefore, you need to perform multiple tests!
Now, how can I add a latency? I have summarized the following methods:
I. Timer 1 var timer: Timer = new Timer (100, 1 );
2 timer. addEventListener (TimerEvent. TIMER_COMPLETE, func );
3 timer. start ();
This method is extracted from a document in http://www.k-zone.cn.
Also, pay attention to garbage collection, that is, timer. stop () in func ();
Ii. setTimeout
I think this method actually calls a tool method. Flash. utils. setTimeout (function (): void {Alert. show ("only show once"); }, 2000, null );
Note that the first parameter must be a function! I personally think this is better and more streamlined!
3. setInterval. It can be repeated.
1 <? Xml version = "1.0" encoding = "UTF-8"?>
2 <mx: Application initialize = "initTimter ()" xmlns: mx = "http://www.adobe.com/2006/mxml" layout = "absolute">
3 <mx: Script>
4 <! [CDATA [
5 import mx. controls. Alert;
6 public var count: Number = 2; // defines the length of time (seconds)
7 public var intervalId: Number; // timer Parameter
8 // timer start Port
9 public function initTimter (): void {
10 intervalId = setInterval (doTimter, 1000 );
11}
12 // timer callback Method
13 public function doTimter (): void {
14 showLabel. text = count. toString () + "second to go ";
15 count-= 1;
16 if (count = 0 ){
17 showLabel. text = count. toString () + "second to go ";
18 count = 2; // start the timer again
19 // clearInterval (intervalId); // remove the timer. Block it to demonstrate the loop
20 Alert. show ("Time is up ");
21}
22}
23]>
24 </mx: Script>
25 <mx: Label id = "showLabel"/>
26 </mx: Application> Code
1 <? Xml version = "1.0" encoding = "UTF-8"?>
2 <mx: Application initialize = "initTimter ()" xmlns: mx = "http://www.adobe.com/2006/mxml" layout = "absolute">
3 <mx: Script>
4 <! [CDATA [
5 import mx. controls. Alert;
6 public var count: Number = 2; // defines the length of time (seconds)
7 public var intervalId: Number; // timer Parameter
8 // timer start Port
9 public function initTimter (): void {
10 intervalId = setInterval (doTimter, 1000 );
11}
12 // timer callback Method
13 public function doTimter (): void {
14 showLabel. text = count. toString () + "second to go ";
15 count-= 1;
16 if (count = 0 ){
17 showLabel. text = count. toString () + "second to go ";
18 count = 2; // start the timer again
19 // clearInterval (intervalId); // remove the timer. Block it to demonstrate the loop
20 Alert. show ("Time is up ");
21}
22}
23]>
24 </mx: Script>
25 <mx: Label id = "showLabel"/>
26 </mx: Application>
The above Code comes from idea.