Forum Moderators: open
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);
}
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
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).