使用時間器區別網頁上的單擊和雙擊

來源:互聯網
上載者:User

標籤:timeout   延遲   script   執行   fun   區別   on()   時間   延時處理   

在網頁上,單擊事件包含一次 onmousedown,onmouseup,onclick的過程。同理,雙擊事件包含了onmousedown,onmouseup,onclick,onmousedown,onmouseup,onclick,ondblclick的過程。所以在網頁上進行ondblclick觸發時同時也會觸發onclick,因此、當我們在同一個element同時註冊了這2個事件時,我們需要將他們區別開來。以下使用延時處理來進行區別。

<script type="text/javascript">

  //當我們單擊時,希望只執行clickAlert

  //當我們雙擊時,希望只執行dblclickAlert

  var timer;
  $(".test").on("click",function(){
    clearTimeout(timer);
    timer = setTimeout("clickAlert()",300); //單擊的處理會延遲0.3秒觸發
  }).on("dblclick",function(){
    clearTimeout(timer);
    dblclickAlert();
  });

  //單擊要做的事情
  function clickAlert(){
    alert("此時為單擊");
  }

  //雙擊要做的事情
  function dblclickAlert(){
    alert("此時為雙擊");
  }
</script>

使用時間器區別網頁上的單擊和雙擊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.