[Javascript] Firing up a function at the end of an event

Billy Reisinger billy.reisinger at gmail.com
Fri May 25 20:18:42 CDT 2007


Terry,
You have it basically right, except for a few small things:

First, you need to call the function (by using the parentheses):
> function fadeIn (id,duration,steps,currstep,MYFINISHINGFUNCTION) {
>   ....
>     MYFINISHINGFUNCTION();
>  ...
> }

...and you also need to change how you are passing the function into  
the fadeIn.  Whenever you use parentheses at the end of a function,  
it is called immediately.  So unless you intend to pass the _result_  
of the function call to fadeIn, what you really want to do is pass  
the function without the parentheses.

//you can define MYFINISHINGFUNCTION first...
var swap = function() {
    swapimage(a,b);
}
//...and then pass it into fadeIn.  Notice no parentheses at the end  
of swap - you don't want to call it, you want to pass it in
fadeIn("currentslide",500,10,10,swap);


//or you can pass an anonymous function... basically you are defining  
the function in the middle of the parameter list for fadeIn.
fadeIn("currentslide",500,10,10,function() { swapimage(a,b); });


Hope that helps!
Billy Reisinger

On May 25, 2007, at 11:29 AM, Terry Riegel wrote:

> Hello All,
>
> Is there a way to have a function call another function when it is  
> done? I have a fadeIn and a fadeOut function that takes place over  
> time. I would like to have it call a passed function when it  
> finishes. I have listed my function below.
>
>
> function fadeIn (id,duration,steps,currstep) {
>   stepDuration = Math.round(duration/steps) ; // Value is in  
> miliseconds.
>   obj = dd.elements[id];
>   obj.setOpacity(1-(1/steps)*currstep);
>   currstep --;
> 	if(currstep>=0){
> 	 setTimeout('fadeIn("'+id+'",'+duration+','+steps+','+currstep 
> +')',stepDuration);
> 	 }
>     else
>      {dd.elements[id].show();
>      }
>   return;
> }
>
>
> I would like to have it work something like this...
>
>
> function fadeIn (id,duration,steps,currstep,MYFINISHINGFUNCTION) {
>   stepDuration = Math.round(duration/steps) ; // Value is in  
> miliseconds.
>   obj = dd.elements[id];
>   obj.setOpacity(1-(1/steps)*currstep);
>   currstep --;
> 	if(currstep>=0){
> 	 setTimeout('fadeIn("'+id+'",'+duration+','+steps+','+currstep 
> +')',stepDuration);
> 	 }
>     else
>      {dd.elements[id].show();
>       MYFINISHINGFUNCTION;
>      }
>   return;
> }
>
>
> I would call it something like...
>
>
> fadeIn("currentslide",500,10,10,swapimage(a,b) );
>
>
> Is this possible? If so what would be the proper syntax for such a  
> convention
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript




More information about the Javascript mailing list