Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't seem to clear timeout?

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi, I have some rotating banners, I rotate them via timeouts...
Code:
// add banner rotate timeout helper
function addBannerRotate(page,pos,ord,timeout)
{    
    var rot = "rotateBanner('"+page+"','"+pos+"',"+ord+")";                       
    BanRotate[pos][ord] = setTimeout(rot,(timeout * 1000));    
}

So I have an object BanRotate, that stores the desired timeouts, this work fine.

However, I want to load a new page via AJAX, clear the old timeouts and assign new ones.

Getting the page works, creating new timeouts is working, but I seem unable to clear the old timeouts and they seem to still run.

I have this code
Code:
    // clear any timeouts
    $(BanRotate).each(function(i,val){
        $(val).each(function(i2,val2){
            clearTimeout(val2);          
        });        
    });

But it isn't' clearing the timeouts?

Any ideas why?

Thanks,
1DMF



"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
OK, solved it.

I changed the JQuery iterator and it's working...

Code:
    // clear any timeouts
    $.each(BanRotate,function(i,val){
        $.each(val,function(i2,val2){
            clearTimeout(val2);          
        });        
    });

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
yup. the first iterates over a jquery collection. the second is a helper function for iterating arrays.

they are confusingly close in name
 
yeah, I find that my brain goes into auto pilot when coding JQuery, and sometimes codes the wrong iterator without thinking.

I then stare at the code thinking why won't you work, it looks fine!

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
if there are some frequent boo-boos then consider writing some abstraction functions that work more like your brain wants them to.

i do that with php a lot. things like isArray() rather than is_array() etc.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top