View Single Post
  #4 (permalink)  
Old June 2nd, 2008, 03:30 PM
Old Pedant Old Pedant is offline
Friend of Wrox
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts

Yes, Joe is right, but it's easy to mess this up.

You *MUST* make the variable that holds the "handle" to the timeout *global* to the page. Too often, people just make it local to the function that holds the call and then it doesn't work.

Also, if a timeout is already active, setting a *new* timeout does *NOT* clear the prior one! So...

var changeImageTimeout = null;

function changeImage()
    // you might not need this, but it's not a bad idea:
    if ( changeImageTimeout != null ) clearTimeout(changeImageTimeout);
    changeImageTimeout = setTimeout('changeImage()',1000);
function stopChangeImage()
    // don't call clear if it's not set...
    if ( changeImageTimeout != null ) clearTimeout(changeImageTimeout);
    changeImageTimeout = null; // tell yourself nothing is active
Reply With Quote