天天看點

Flex4之定時器之setInterval應用(定時至某個時間發起,可循環)

Flex4定時器我們用得最多的是Timer定時器,但Timer一初始化的時候就會調用,這裡我們使用一個比較好的定時器,讓其經過指定時間後再進行某項操作,比如重新整理界面的操作。

下面我來介紹它的實作,不用多說,附上代碼。

首先在init函數中進行初始化。

public function initFunc():void
			{
				count = 0;
				interval = setInterval(countDown,1000);
				// setInterval(this,"countDown", 1000);
			}
           

然後實作countDown函數:

public function countDown():void
			{
				showLabel.text = count.toString() + "second to go";
				count += 1;
				
				if(count==10){
					count=0;
					timeUp();
				}
           

再實作timeUp函數:

public function timeUp():void
			{
				//clearInterval(interval);
			//	Alert.show("時間到了。");
				navigateToURL(new URLRequest("javascript:location.reload();"),"_self");
				//重新整理頁面
				
			   //	navigateToURL(new URLRequest("javascript:location.close();"),"_self") 
				//關閉頁面操作
				
				//也可以重開頁面
				
			}
           

當然要在Application标簽中引上上面的那個方法,如下所示:

<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" minWidth="955" minHeight="600"
			   initialize="initFunc()"
			   >
           

這樣每經過10就會重新整理一次頁面了。

繼續閱讀