A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: call a function after a certain amount of time

  1. #1
    Senior Member
    Join Date
    Jul 2008
    Posts
    418

    call a function after a certain amount of time

    Hello,

    I have a piece of code, and in that function i call to another function that has 3 parameters.

    now i've decided that i want that last function to be called after a couple of secs, instead of immedietly. I can't simply use a timer, and addEventlistener, because then i cant give parameters.

    Does anyone know the solution to this?
    I program in AS3 only.

  2. #2
    Senior Member
    Join Date
    Jul 2006
    Location
    San Jose, CA
    Posts
    334
    Well, all searches came up negative for something like this, so the only thing I can think is something along this line:

    Code:
    var t:Timer = new Timer(2000);
    t.addEventListener(TimerEvent.TIMER, onDelay);
    t.start()
    
    function onDelay(te:Event):void {
        doCall();
    }
    function doCall():void {
        yourFunction("Trace", "Hello", "World!");
    }
    You may event be able to do so by calling the function directly:

    Code:
    var t:Timer = new Timer(2000);
    t.addEventListener(TimerEvent.TIMER, function():void { yourFunction("Trace", "Hello", "World!" });
    t.start()
    I (Love | Hate) Flash.
    ----
    Save a version back so others may help you!

  3. #3
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    PHP Code:
    var t:Timer = new Timer(2000);
    t.addEventListener(TimerEvent.TIMER, function():void yourFunction("Trace""Hello""World!" });
    t.start() 
    can, i then instead of yourFunction("trace","hello","world"}
    do:
    PHP Code:
    var a:String "trace",b:String "hello",c:String "world!";
    t.addEventListener(TimerEvent.TIMER, function():void yourFunction(ab}); 
    I program in AS3 only.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The anonymous function you're using should take the TimerEvent as a parameter. Other than that, it looks good.

  5. #5
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    Ok great.
    another question, that's related: Is it possible to alter that function dynamically?
    so basically you're saying:
    PHP Code:
    if something
    {
    add this statement to the function: x+=2
    }else
    {
    add this statement to the function: goToBeginning();


    edit: a problem i've encountered:
    PHP Code:
    trace("nx: " +nX +"   nY: " nY+"   length: "+currentMatchArrayLength);//traces 300,400,3
    var nFunction:Function =
    function(
    e:TimerEvent):void
    {
        
    processScore(nX,nY,currentMatchArrayLength);
        
    trace("nx: " +nX +"   nY: " nY+"   length: "+currentMatchArrayLength);//traces 300,400,1 why is currentmatcharraylength suddenly 1, but the others are the same?
        

    edit: is it possible that if you do this:
    Code:
    blabla:int = 10;
    for(i=0;i<5;i++)//pseudocode
    {
          blabla+=5
          nFunction = function(e)
          {
                myFunction(blabla);
          }
          timer.addEventListener(timer,nFunction);
          timer.start();
    }
    is it possible that this function will only be executed once?
    What i want is the function to be executed once with blabla 10, once with 15 once with 20 once with 25 etc.
    but I think that this isn't happening?

    is there perhaps a maximum amount of timers, or functions?
    Last edited by omniscient232; 04-08-2009 at 05:04 AM.
    I program in AS3 only.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    To answer your first question, no you can't alter the content of a function after it's defined. You could wrap it in a new function:
    Code:
    var oldNFunction:Function = nFunction;
    nFunction = function(e:TimerEvent):void{
      oldNFunction(e);
      x+=2;
    }
    But be aware of subtle scoping issues, like if you've already added nFunction as a listener at that point, the new function is not the listener you've added.

    The second question is more subtle scoping issues. I think what should happen in that case is that the timer will have 5 listeners, but they will all be using the last blabla value, because blabla is scoped outside of the function. To capture it within the function, you'd have to use another level of indirection:
    Code:
    blabla:int = 10;
    for(i=0;i<5;i++)//pseudocode
    {
          blabla+=5
          nFunction = function(b:int):Function {
            return function(e:Event):void
             {
                myFunction(b);
             };
           }(blabla);
          timer.addEventListener(timer,nFunction);
          timer.start();
    }

  7. #7
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    I don't think i understand the last answer.
    what is this line for:
    PHP Code:
    }(blabla); 
    where is the parameter b inserted?
    I program in AS3 only.

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your questions answer each other.
    Code:
    }(blabla)
    indicates that we are closing up the definition of the outer anonymous function, and immediately calling it with blabla as an argument. It will return a function where b is bound to the value of blabla at the time the outer function was called.

  9. #9
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    so nFunction then contains the inner function?
    I program in AS3 only.

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Well, yes and no. It contains a function very similar to the inner function, but the variable b is bound to the value that blabla had when the outer function was called. So, on the first loop through, the inner function is equivalent to
    Code:
    function(e:Event):void{
      myFunction(10);
    }
    And in the second time through, it's a different function equivalent to:
    Code:
    function(e:Event):void{
      myFunction(15);
    }
    Etc.

  11. #11
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    but then if you do the:
    PHP Code:
    }(blabla); 
    how does it work? you said the function was immedietly called?
    does that mean that nFunction now contains the return value of the outer function, which is the inner function?
    I program in AS3 only.

  12. #12
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yes. The outer function is immediately called, and returns the inner function.

  13. #13
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    ok.
    Thanks alot!
    I program in AS3 only.

  14. #14
    Junior Member
    Join Date
    Apr 2010
    Posts
    1

    Whats wrong with this code:

    hello, What wrong with this code:

    Code:
    
    var t:Timer = new Timer(5);
    t.addEventListener(TimerEvent.TIMER, onDelay);
    t.start()
    
    function onDelay(te:Event):void {
        
    
    	var loader:URLLoader = new URLLoader();
    	loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    	var phpFileLocation:String = "1.php";
    	var urlRequest = new URLRequest(phpFileLocation);
    	loader.load(new URLRequest(phpFileLocation));
    	loader.addEventListener(Event.COMPLETE, onDataLoad);
    	
    	function onDataLoad($event:Event)
    	{
    		for(var i:int = 0; i < 1; i++)
    		{
    			var returned:String = $event.target.data["authenticated"];
    			//Test if it's a string:
    			trace(typeof returned);
    			//See the length of the string:
    			trace(returned.length);
    			//Loop through string to see the actual characters:
    			for(var j:int = 0; j < returned.length; j++)
    			{
    				trace(j + " = " + returned.charAt(j));
    			}
    			
    			if(returned == "1")
    			{
    				resultado.text = "Logged";
    			}
    			else if(returned == "0")
    			{
    				resultado.text = "Go away";
    			}
    		}
    	}
    
    
    }

  15. #15
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You are creating a new URLLoader and making a request to your php file every 5 milliseconds. That's far too often, and you really shouldn't have two loaders for the same resource at the same time. You are not testing for any error conditions. You've defined onDataLoad inside onDelay for no reason, causing each loader to have a reference to a different function doing the exact same thing.

    And you never told us what behavior you want or what behavior you're getting.

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