search

Home  >  Q&A  >  body text

javascript - Problem with variable assignment in timer

var a=0;
var timer=setTimeout(function(){
    a++;
},1000);
if(a>0){
    alert("1");
}

I clearly changed the value of a in this code in the timer, why is the output still 0?
How should I write to make this pop-up window pop up? (Based on making the judgment effective. )

过去多啦不再A梦过去多啦不再A梦2789 days ago570

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-05-19 10:36:41

    The execution sequence you imagined

    var a = 0;
    a++;
    console.log(a);

    Actual execution sequence

    var a = 0;
    console.log(a);
    delay 1 second ……
    a++;
    

    There are many ways to get this a=1, but you don’t know the meaning here, what is it for.

    If you have additional questions, please put them in the question, not in the comments of the question.

    reply
    0
  • Cancelreply