var mark2=true;
if(mark2){
move(1);
mark2=false;
}
function move(){
$(".box").animate({
width: arrW[index],
height: arrH[index],
opacity: arrO[index],
left: arrL[index],
top: arrT[index]
},500,function(){
mark2=true;
})
}
以上代碼執(zhí)行move(1); mark2=false;
這兩句的時(shí)候,move
函數(shù)中用了animate
動畫函數(shù),那move
的調(diào)用是屬于異步的嗎?也就是放到任務(wù)隊(duì)列中執(zhí)行嗎,所以首先執(zhí)行mark2=false;
這樣理解對嗎?
走同樣的路,發(fā)現(xiàn)不同的人生
我看這個題主你可以直接在代碼上寫上console.log('')
打印內(nèi)容來驗(yàn)證你猜想的順序的。
jquery 的animate 是異步的,這個不用說吧,http://www.cnblogs.com/aaronj...
一般原理都是利用setTimeout之類的來定時(shí)延遲執(zhí)行,顯然animate的回調(diào)到點(diǎn)會放到任務(wù)隊(duì)列里,所以mark2=false
肯定先執(zhí)行了
調(diào)用move肯定是同步的阻塞的,
animate也是同步阻塞的
$(document).ready(function () {
var mark2 = true;
if (mark2) {
move(1);
console.log('運(yùn)行結(jié)束')
}
})
function move() {
console.log("move start")
$(".box").animate({
width: 50,
height: 50,
opacity: 30,
left: 200,
top: 200
}, {
duration: 1500,
start: function () {
console.log("animate start")
},
complete: function () {
console.log("animate end")
}
})
console.log("move end")
}
結(jié)果是
first:25 move start
first:37 animate start
first:44 move end
first:20 運(yùn)行結(jié)束
first:40 animate end
如果move不是同步的
你會先看到“運(yùn)行結(jié)束”然后才是其他的東西
如果animate不是同步的
你會看到move end 在 animate start 前頭。
比如
$(document).ready(function () {
var mark2 = true;
if (mark2) {
move(1);
console.log('運(yùn)行結(jié)束')
}
})
function move() {
console.log("move start")
setTimeout(function () {
$(".box").animate({
width: 50,
height: 50,
opacity: 30,
left: 200,
top: 200
}, {
duration: 1500,
start: function () {
console.log("animate start")
},
complete: function () {
console.log("animate end")
}
})
}, 500)
console.log("move end")
}
結(jié)果是
first:25 move start
first:45 move end
first:20 運(yùn)行結(jié)束
first:36 animate start
first:39 animate end