views:

958

answers:

1

Hi,

Ok thanks to all of you I have made my Toggle() function that alternates an image (changes the images to different colors when pressed).

I now want to animate the images, so basically call my toggle() function, but leave a time delay between calls to make it look like a transition/animation.

What would be the best way to do this?

note: I already have the function code, I just need a way to call the function with a timedelay etc

+1  A: 

The way to implement a time delay in Javascript is with set timeout:

setTimeout(function(){alert("hi")}, 3000);

The first argument is a function that will be executed after the delay (you can either pass an anonymous function as shown here or a named one on some object like: "MyThing.doSomething"). The second argument is the desired delay in milliseconds.

For a detailed analysis of timers in Javascript, see John Resig's How Javascript Timers Work.

Greg Borenstein