English 中文(简体)
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.

最佳回答

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.

问题回答

Try this:

setTimeout(function() { window.link = true; }, 5000);

This will set the global variable "link" to true after 5 seconds, which will satisfy your if statement.

Edit This may be a bit complicated if you re a beginner, but a better way to accomplish this is to use function-scope rather than global scope.

In your case, declare the timer function like this:

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

    return function() {
        alert(link);
    };
}());

This way, the anonymous function returns another function which becomes timer(), but this way timer has access to its "private" link variable. For more information, check out Mozilla s article on JavaScript variable scope

Simple one-liner timeout:

setTimeout(() => alert("Time is up!"), 1000);

You can obviously execute any code within:

let seconds = 5;
let timer = setInterval(() => console.log(`${seconds--} seconds left`), 1000);

setTimeout(() => {
  clearInterval(timer);
  console.log( ? )
}, 5999);




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签