A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: [F8] How to delay the execution of code?

  1. #1
    Senior Member
    Join Date
    Mar 2007
    Posts
    133

    [F8] How to delay the execution of code?

    Hey guys,

    Just wondering if anyone can explain how to delay the execution of code? I'm using this code to enable some buttons that have been disabled during an external swf loading. The thing is, i dont want them to be selectable exactly as soon as the swf has loaded, i'd rather delay them to let the swf animate in first (to prevent the user starting the swf "_out" animation before it has loaded in).

    Anyway, should i use setInterval? Im not sure because im only executing this code one time, i just want it to be delayed about 20 frames (~0.67 secs). Whats the simplest way to do this?

    Thx

  2. #2
    A couple things you could do: put the button enabling code inside the buttons on the frame that you want them to be selectable. Or, to keep it on the main timeline, I believe you could use onClipEvent(enterFrame) to check what frame the button is on, and enable it if it's at a certain frame.

  3. #3
    Senior Member
    Join Date
    Mar 2007
    Posts
    133
    Thx for the reply, but im really looking for something that will say execute this code in a certain period of time. Similar to the setInterval but not repeating (or does someone know how to do this with the setInterval?). Checking all the frames wouldn't be as efficient i dont think, but i'll give it a shot if i have to.

  4. #4
    Senior Member Speed85's Avatar
    Join Date
    Apr 2007
    Posts
    292
    setInterval would work fine, i can't say 100% if there is anything better, but all you have to do is clear the interval, try this code:

    code:

    function someName() {
    //insert code here
    clearInterval(intervalId)
    }
    intervalId = setInterval(this, "someName", time);

    Whoever taught EVERYONE to put all their code on movie clips needs to be shot...

  5. #5
    Senior Member
    Join Date
    Mar 2007
    Posts
    133
    The problem i get with setInterval is that the code keeps repeating over and over again. I can't clear it properly. The main reason being because the setInterval is initialised inside the onLoadProgress.

    Here's my code:


    //---------- External MCLoader Preloader--------->
    var mcLoader:MovieClipLoader = new MovieClipLoader();
    var myListener:Object = new Object ();
    mcLoader.addListener(myListener);
    var firstLoad:Boolean = true; //use so that the onLoadProgress doesnt need to repeat code

    myListener.onLoadProgress = function(target_mc,bytesLoaded,bytesTotal){

    if(firstLoad == true){
    //disable buttons
    symbolHit_mc.gotoAndStop("_off");
    aboutHit_mc.gotoAndStop("_off");
    service****_mc.gotoAndStop("_off");
    portfolioHit_mc.gotoAndStop("_off");
    download****_mc.gotoAndStop("_off");
    quoteHit_mc.gotoAndStop("_off");
    contactHit_mc.gotoAndStop("_off");

    if(currentPage == "home"){
    box1Hit_mc.gotoAndStop("_off");
    box2Hit_mc.gotoAndStop("_off");
    box3Hit_mc.gotoAndStop("_off");
    box4Hit_mc.gotoAndStop("_off");
    contactU****_mc.gotoAndStop("_off");
    requestAQuoteHit_mc.gotoAndStop("_off");
    }
    firstLoad = false;
    }
    if(bytesLoaded >= bytesTotal){
    //enable buttons after page has loaded in

    var loadingInterval = setInterval(_root, "enableLoadingInterval", 500);
    firstLoad = true;
    }
    }
    //enable buttons
    function enableLoadingInterval() {
    symbolHit_mc.gotoAndStop(1);
    aboutHit_mc.gotoAndStop(1);
    service****_mc.gotoAndStop(1);
    portfolioHit_mc.gotoAndStop(1);
    download****_mc.gotoAndStop(1);
    quoteHit_mc.gotoAndStop(1);
    contactHit_mc.gotoAndStop(1);

    if(currentPage == "home"){
    quoteHit_mc.gotoAndStop(1);
    contactHit_mc.gotoAndStop(1);
    box1Hit_mc.gotoAndStop(1);
    box2Hit_mc.gotoAndStop(1);
    box3Hit_mc.gotoAndStop(1);
    box4Hit_mc.gotoAndStop(1);
    contactU****_mc.gotoAndStop(1);
    requestAQuoteHit_mc.gotoAndStop(1);
    }
    firstLoad = true;
    clearInterval(loadingInterval);
    }



    I'm using invisible buttons (anything with Hit_mc). How can i clear the interval? Thx

  6. #6
    Member
    Join Date
    Nov 2003
    Location
    Ontario, Canada
    Posts
    80
    Code:
    function wait(var amount:Number) { 
    	var myInterval = setInterval(function(){clearInterval(myInterval), numberOfSeconds * 1000/amount); 
    }
    where ever you need to wait just call this function;

    Maybe you could help me with my post?

    "[F8] Flash to Flash"

  7. #7
    Senior Member
    Join Date
    Mar 2007
    Posts
    133
    Thx gemione looks promising but i can't get it to work. I implemented it as:

    function wait() {
    var myInterval = setInterval(function(){clearInterval(myInterval)}, 0.7*1000);
    }


    and i called wait(); when bytesLoaded >= bytesTotal, and then included the code underneath it.

    How does this code actually work? Like what do i need to substitute and where do i put the enable buttons code? Thx

  8. #8
    Member Pepember
    Join Date
    Jul 2001
    Location
    Berlin
    Posts
    886
    there is a function called setTimeout();
    usage:
    setTimeout(function, milliseconds);

    it gets executed just once after the given milliseconds have passed.
    it's available since Flash Player 8. see more info here:
    http://livedocs.adobe.com/flash/9.0/...=00001218.html
    Please sign here

  9. #9
    Senior Member
    Join Date
    Mar 2007
    Posts
    133
    LEGEND! Thanks so much for the setTimeout() function. Works perfect. Exactly what i was looking for.

  10. #10
    Senior Member Speed85's Avatar
    Join Date
    Apr 2007
    Posts
    292
    heh nice, I gotta remember that one
    Whoever taught EVERYONE to put all their code on movie clips needs to be shot...

  11. #11
    Member Pepember
    Join Date
    Jul 2001
    Location
    Berlin
    Posts
    886
    sure, no problem...just discovered the little gem only recently myself ;-)
    Please sign here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center