jquery延遲處理和定時執行代碼例子

來源:互聯網
上載者:User

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()的隊列當中。

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.