class TimeTracer { private var timerInterval:Number; public function startTimeDisplay():Void { stopTimeDisplay(); var begunAt:String = new Date().toString(); timerInterval = setInterval(displayTime, 1000); function displayTime():Void { trace("Time now: "+new Date().toString()+". "+"Timer started at: "+begunAt); } } public function stopTimeDisplay():Void { clearInterval(timerInterval); } } /* 当stopTimeDisplay( )被调用时,间隔被停止,然后嵌套函数displayTime( )被自动删除。 而且,被保存的用于displayTime( )的局部变量begunAt也不再需要,因此被自动地垃圾回收 garbage-collected(即,删除)。 */ /* usage: t = new TimeTracer(); t.startTimeDisplay(); trace Time now: Wed Nov 30 14:18:55 GMT+0800 2005. Timer started at: Wed Nov 30 14:18:54 GMT+0800 2005 Time now: Wed Nov 30 14:18:56 GMT+0800 2005. Timer started at: Wed Nov 30 14:18:54 GMT+0800 2005 Time now: Wed Nov 30 14:18:57 GMT+0800 2005. Timer started at: Wed Nov 30 14:18:54 GMT+0800 2005 ...... ...... */