Flash 8 setTimeout Example

Originally blogged by Guy Watson, the setTimeout() function is an undocumented feature in Flash 8. This function can come in really handy for things like tooltips. See the sample app below where you can enter in any number for the delay. The actionscript is also posted below.

[as]tip._visible = false;
var isOn:Boolean = false;

butt.onRollOver = function() {
setTimeout(showTip,milli.text);
isOn = true;
}

butt.onRollOut = function() {
tip._visible = false;
isOn = false;
}

function showTip() {
if(isOn) {
tip._visible = true;
}
}[/as]


Commentary

  1. ericd says:

    if you use it in a Class, use _global.setTimeout or it might not work.

  2. Administrator says:

    Great thanks Eric!

  3. Tim Wilson says:

    Sorry, It didn’t work for me.

    I have the Latest Version of Flash runnng in Safari on mac osx :-(
    Otherwise it would be a useful funtion.

  4. Tim Wilson says:

    Idiot, It times in not out. D’oh, It does work.
    Can’t believe I missed that.

    Cool, I’ve made a note of this.

  5. Anyone know why this was left undocumented? This replaces setInterval for more cases than I care to count.

  6. Do I see this right, this function can save a crapload of setInterval routine code?! I think this undocumented function will be very useful!

  7. Administrator says:

    Yep! It’s hard to believe that Macromedia would hide such a useful function.

  8. pete says:

    doenst work in the 8.5 player

  9. Quentin says:

    Hi !
    I’ve read somewhere that this function is not ‘undocumented’ but ‘has been forgotten when writing the docs’…

    However, very useful one !
    Instead of using _global.setTimeout, you can edit ‘toplevel.as’ in your Configuration/Classes folder and add :

    intrinsic function setTimeout():Number;

    Cheers !

  10. sycamore says:

    nice job man!!!!!!

  11. It actually seems that it works in actionscript 1 as well. (It did for me)
    That brings us to the question; How long has this excellent little snippet been in there?

  12. jqln says:

    quentin, you saved my life.

  13. tutash says:

    It has been a pain for me to do this with setInterval. I used to do it something like this:

    intId = “ID” + i.toString()
    this[intId] = setInterval(something,100,intId)

    something = function(whichId){
    trace(“DOING IT”)
    clearInterval(eval(whichId))
    }

  14. TBolt says:

    Just out of curiosity I looked into your swf for this example and you’ve got almost 9000 lines of code in there. Funny.

    Also for some reason or another I getting errors when trying to do this.

    Anyhow, will be cool when I do get it working.

  15. beHzad says:

    Great!
    thank you,
    this was so useful for me…

    thanks again.

  16. tonpcom says:

    BrokenCitadel: The function may seem to work on

  17. Dani says:

    This whole deal is rather funky… I have tried various options, BUT the only version that worked for me was

    _global['setTimeout'](my_function, 1500);

    Plain and simple.

  18. phil says:

    Just to throw this in as well – there is a clearTimeout().

    not the prettiest code, but a simple example :)

    // copy and paste in frame – make a movieclip named ‘b2′
    //and one named ‘b1′
    // tempbutton2 – rollover and releaseoutside
    b2.onRollOver= function() {
    gome()
    };
    b2.onRollOut = b2.onReleaseOutside = function() {
    killme()
    };
    // start the timer
    function gome() {
    dog = setTimeout(visime,1000,true);
    }
    // kill the timer
    function killme () {
    clearTimeout(dog)
    visime(false)
    }
    // do stuff
    function visime(x) {
    b1._visible = x
    }
    // turn off ‘b1′ to at start…
    visime(false)

    hope this helps someone
    phil

  19. jeff proehl says:

    Hey:

    I’m wondering if anyone can help me out? I’m trying to
    implement a timeout feature in Flash that restarts the
    timer every mouse click. This is for science museum diaplay, and I want to have it such that after the thing
    sits idle for 5 minutes it jumps back to the start. What
    I’ve tried in the first frame is:

    stop();
    var mouseListener:Object;
    mouseListener=new Object();
    mouseListener.onMouseDown = function() {
    if(timer1 != null) {
    clearTimeout(timer1);
    }
    var timer1=setTimeout(restart,15*1000);
    };
    Mouse.addListener(mouseListener);

    function restart()
    {
    _root.gotoAndStop(1);
    }
    intro_mc.onRelease = function()
    {
    gotoAndStop(“story”);

    }

    The tag “story” is page 2 that enters into the content.
    The content has several buttons defined that are all set
    up “onRelease” so the onMouseDown shouldn’t interact with
    those (or will it?). When I try the above code, the first instance works, but soon I get this jumping back
    at odd (shorter) time intervals. Almost like several timers may be running.

    Any help would be greatly appreciated, the museum wants
    this last week! Thanks!

    Jeff

  20. nick ganga says:

    I need the exact same solution Jeff is asking for. Anyone?

  21. ADI says:

    if u want use it in class u have to add proper variables to this function.

    do it like that:

    _global.setTimeout(this,”function_name”,miliseconds);

  22. albertos says:

    u can user that to implement a sleep result. my implemantation:

    for (i=0;i

  23. albertos says:

    damn…

    for (i=0;i&lt10;i++)
    {
    setTimeout(myfunc, i * 100, args);
    }

    function myfunc(args)
    {
    DoSomething…
    }

  24. albertos says:

    @jeff proehl

    did u try:

    if (timer1 != undefined)
    {
    clearTimeout(timer1);
    }

  25. Untitled says:

    Undocumented? No wonder I couldn’t find it in the Help files…

    I had used setTimeout() in Javascript before so I tried using it, given the similarities between Javascript and Actionscript. So, I found it odd when it wasn’t syntax-coloured by Flash, since setInterval() worked. I turned to the help files and found nothing. I had thought Macromedia probably removed it in favour of something else (although I didn’t know what).

  26. seutje says:

    @ jeff proehl

    should always make sure timers are set on _global or u will be unable to clear them unless u clear virtually all timeouts by using something like:
    for (i:Number = 0; i < 9999; i++)
    {
    clearTimeout(i);
    }

    … but that’s just retarded and asking for trouble

    in ur case, try changing “var timer1 = setTimeout(….” to “_global.timer1 = setTimeout(…”

    and also change the “clearTimeout(timer1)” to “clearTimeout(_global.timer1)”

    I mostly make interactive slideshows for big corporations, and they always want a sorter with it, which is basicly just a flash projector where u can import separate slides in swf format and organize them, export them into 1 big swf and even print each slide as a bitmap

    I stumbled upon this problem when I noticed that if I made a standalone version of a presentation, it worked perfectly, but if I loaded the slides up in the sorter and previewed it, none of the timeouts were being cleared, which resulted in certain animations popping up 7 times instead of just once, if u would scroll trough the slides really fast

    my fix (doubt any1 will get any smarter from this though :p):

    function makePlay(ani:MovieClip)
    {
    ani.gotoAndPlay(2);
    }

    for (i:Number = 0; i <= 10; i++)
    {
    clearTimeout(_global["timeoutAni" + (i +1)]);
    }

    _global.timeoutAni1 = setTimeout(makePlay, 3000, ani1);
    _global.timeoutAni2 = setTimeout(makePlay, 6000, ani2);
    _global.timeoutAni3 = setTimeout(makePlay, 9000, ani3);
    _global.timeoutAni4 = setTimeout(makePlay, 11000, ani4);
    _global.timeoutAni5 = setTimeout(makePlay, 13000, ani5);

  1. [... Originally blogged by Guy Watson, the setTimeout() function is an undocumented feature in Flash 8. This function can come in really handy for things like tooltips. See the sample app ...]

  2. [... Originally blogged by Guy Watson, the setTimeout() function is an undocumented feature in Flash 8. This function can come in really handy for things like tooltips. See the sample app ...]

Leave a Comment