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
動(dòng)畫函數(shù),那move
的呼叫是屬於非同步的嗎?也就是放到任務(wù)佇列中執(zhí)行嗎,所以先執(zhí)行mark2=false;
這樣理解對(duì)嗎?
走同樣的路,發(fā)現(xiàn)不同的人生
我看這個(gè)題主你可以直接在程式碼上寫上console.log('')
印內(nèi)容來驗(yàn)證你猜想的順序的。
jquery 的animate 是異步的,這個(gè)不用說吧,http://www.cnblogs.com/aaronj...
一般原理都是利用setTimeout之類的來定時(shí)延遲執(zhí)行,顯然animate的回調(diào)到點(diǎn)會(huì)放到任務(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不是同步的
你會(huì)先看到「運(yùn)行結(jié)束」然後才是其他的東西
如果animate不是同步的
你會(huì)看到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