How to set a variable after x seconds?

前端 未结 3 1951
清歌不尽
清歌不尽 2020-12-20 23:23

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 t

3条回答
  •  梦毁少年i
    2020-12-20 23:58

    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.

提交回复
热议问题