Flash 8 - setTimeout

Yup, you heard it here first. In Flash 8 actionscript you can tell the player to call a function just once after a specified interval. This means that you dont have to use setInterval anymore to call a function just once. For example the following code calls the openWebsite function after 1 second (1000 milli-seconds) and passes it a url. The openWebsite function then opens that url in a new browser window.

Actionscript:
  1. function openWebsite(url:String)
  2. {
  3. getURL(url,'_blank')
  4. }
  5. website='http://www.flashguru.co.uk'
  6. setTimeout(openWebsite,1000,website)

And of course you can use the following syntax aswell:

Actionscript:
  1. function openWebsite(url:String)
  2. {
  3. getURL(url,'_blank')
  4. }
  5. website='http://www.flashguru.co.uk'
  6. setTimeout(this,'openWebsite',1000,website)

Yet another one of those small features that make our lifes as Flash Developers just that little bit easier :)

Note: That to use the setTimeout function in Actionscript 2 classes, you will need to use array notation to avoid compiler errors as this function was left out of the intrinsic class definition files:

Actionscript:
  1. //call the showPopup function once after a second (1000 ms)
  2. _global['setTimeout'](this,'showPopup',1000);

2 Responses to “Flash 8 - setTimeout”

  1. Nick says:

    Wow, a very nice undocumented feature ;) Any others I haven’t heard about?

  2. mark mun says:

    if you use setTimeout in a AS 2.0 class, you can also write it without the bracket : _global.setTimeout(this,’showPopup’, 1000)