Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

这段代码中a的值我明明在定时器里改变了,为啥输出的还是0?
应该怎么写才能让这个弹窗弹出来呢?(在让判断生效的基础上.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
298 views
Welcome To Ask or Share your Answers For Others

1 Answer

你想象中的执行顺序

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

实际的执行顺序

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

拿到这个a=1的方法有很多,但是你这里意义未明,到底是为了什么。

还有问题的补充要放在问题里,不要放在问题的评论里。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...