Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a variable after x seconds?

I'm basically trying to accomplish the following. I want it so 5 seconds after the page loads, it'll set the variable to true.

Once true, it'll proceed to give the alert "true".. for now. If someone tries to click the button before 5 seconds, it'll give the alert false.

like image 431
Ricky Avatar asked Nov 16 '09 07:11

Ricky


1 Answers

You've got the right idea, but you have a minor issue with variable scope. To reduce headaches, it's really best to get away from using the string eval option on setTimeout (which is shown in tutorials all around the web, I know) and use an anonymous function:

var link;
function loading(){
    setTimeout(function(){ 
        link = true; 
    }, 5000);
}

This way, you'll know exactly where link is declared and the scope is crystal clear.

like image 197
brianreavis Avatar answered Sep 28 '22 06:09

brianreavis