Forum Moderators: open

Message Too Old, No Replies

window.setTimeout in Mozilla - Aaargh!

         

username

3:28 am on Oct 9, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi All, I am doing a pretty basic show/hide using a setTimeout feature. This works perfectly in IE, but does not in Mozilla. I have been all through the Mozilla developer center, in particular:

[developer.mozilla.org...]

I have tried everything, including calling functions as the call value etc, but cannot understand why it will not execute the hide loader line. I have:

function loader(sharedLoader,sharedValues){
var sharedLoader = document.getElementById(sharedLoader);//loader div
var sharedValues = document.getElementById(sharedValues);//loader content
var timeoutLoader = window.setTimeout("sharedLoader.style.display = 'none'",2000); //will not run
var timeoutShared = window.setTimeout("sharedValues.style.display = ''",2050);
}

Little_G

11:25 am on Oct 9, 2008 (gmt 0)

10+ Year Member



Hi,

Try using anonymous functions:

function loader(sharedLoader,sharedValues){
var sharedLoader = document.getElementById(sharedLoader); //loader div
var sharedValues = document.getElementById(sharedValues); //loader content
var timeoutLoader = window.setTimeout(function(){sharedLoader.style.display = 'none';}, 2000);
var timeoutShared = window.setTimeout(function(){sharedValues.style.display = '';}, 2050);
}

Andrew

Fotiman

3:20 pm on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The reason this line does not work:
var timeoutLoader = window.setTimeout("sharedLoader.style.display = 'none'",2000); //will not run

is because you're passing a string to the setTimeout method. When setTimeout tries to evaluate that string, "sharedLoader" is not going to be defined, which is why it doesn't work. The anonymous function approach should work, though, because of closures. The anonymous function refers to the sharedLoader variable instead of a string, so when setTimeout runs it knows what to operate on already (thanks to closures).

username

11:11 pm on Oct 9, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



10/10 everyone. Thank you so much. My site feature is so much cooler than before now it works, and thanks to you. It's always something simple. :)