In the last days I’ve received a lot of questions about how to automatically close a popup window after a certain period of time. In order to demonstrate it let’s implementing a Flex alert that will be self-closed after 5 seconds. Check the code after the jump:
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx"> <fx:Script> [CDATA[ import mx.controls.Alert; import mx.managers.PopUpManager; ? private var notificatorTimer:Timer; private var myAlert:Alert; ? [Bindable] public var timeDestroy:Number = 5000; ? ? private function startTimer(event:MouseEvent):void { notificatorTimer = new Timer(timeDestroy, 1); notificatorTimer.addEventListener( TimerEvent.TIMER_COMPLETE, remove_handler); notificatorTimer.start(); myAlert = Alert.show( "Wait, I'm gonna be closed after 5 sec ...", "MyAlert"); } ? ? private function remove_handler(event:TimerEvent):void { PopUpManager.removePopUp(myAlert); } ? ]]> fx:Script> ? <s:Button label="Abrir Alert" click="startTimer(event)" /> ? s:Application> |
Hope you enjoy it!




