1、不應該用sleep,javascript又沒有多線程,sleep了就阻塞了。
可以參考這個問題說明js是單線程的原理。
應該換個思路,用setInterval來實現迴圈。.
| 代碼如下 |
複製代碼 |
$("#list div.left").mouseover(function(){
var item=$(this).attr("id");
//alert(item);
t = setTimeout(function(){
$("#"+item).parents("div.oitem").animate({width:"292"},400);
$("#"+item).parents("div.oitem").siblings("div").animate({width:"126"},400);
},100);
}).mouseout(function(){
clearTimeout(t);
}); |
2、在jquery 1.4中新增了一個叫delay(time);也可以現實順延強制 time為延遲時間 1000=1s
| 代碼如下 |
複製代碼 |
|
$("button").click(function() {
$("div.first").slideUp(300).delay(800).fadeIn(400);
$("div.second").slideUp(300).fadeIn(400);
});
.delay( duration [, queueName] ) |
函數可直接用於動畫過程中的延時環節,不過它沒有callback參數,不能直接用來做定時器。
Added to jQuery in version 1.4, the .delay() method allows us to delay the execution of functions that follow it in the queue. It can be used with the standard effects queue or with a custom queue. Only subsequent events in a queue are delayed; for example this will not delay the no-arguments forms of .show() or .hide() which do not use the effects queue.
這是JQuery文檔裡對.delay()方法的說明,也就是只有後延的事情才會被加入delay()的隊列當中。