2015-06-30 24 views
22

Tôi có một div và tôi muốn kích hoạt sự kiện chỉ sau khi người dùng liên tục di chuột của mình trong 3 giây. Mã của tôi không hoạt động tốt vì nó kích hoạt ngay sau khi di chuột và không "chờ".Cách kích hoạt sự kiện sau 3 giây di chuột

Code:

$(".inner_pic").mouseenter(function() { 
    setTimeout(function() { 
     alert('testing'); 
    }, 3000); 
}).mouseleave(function() { 
    alert('finish'); 
}); 
+0

Hiển thị thêm mã. Tạo [MCVE] (http://stackoverflow.com/help/mcve) – Amit

+0

http://jsfiddle.net/nn33vwvn/ dường như có tác dụng với tôi. Những gì có vẻ là vấn đề? –

Trả lời

24

Bạn cần lưu trữ id timeout đâu đó và xóa nó khi di chuột ra. Đó là thuận tiện để sử dụng tài sản dữ liệu để lưu id này:

$(".inner_pic").mouseenter(function() { 
 
    $(this).data('timeout', setTimeout(function() { 
 
     alert('testing'); 
 
    }, 3000)); 
 
}).mouseleave(function() { 
 
    clearTimeout($(this).data('timeout')); 
 
    alert('finish'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="inner_pic">PICTURE</div>

+2

Lưu ý thú vị: Với Internet Explorer 11 (có thể là các trình duyệt/phiên bản khác) kích hoạt cảnh báo, sau đó di chuột qua cảnh báo bằng chuột sẽ khiến bạn mở nhiều hơn. – ThePyroEagle

+1

@ ThePyroEagle Wow, đây là trình duyệt rất lạ! Hóa ra rằng IE11 kích hoạt sự kiện mouseout trong trường hợp này mặc dù cảnh báo ở trên đầu, điều này sẽ ngăn các tương tác tiếp theo với trang. Lạ lùng. – dfsq

11

Bạn có thể đạt được điều này bằng cách delay tùy chọn:

Working demo

$('#elem').popover({ 
    trigger: "hover", 
    delay: {show : 3000, hide : 0} }); 
+1

Không có trong jQuery. Không có 'popover' jquery. –

+0

@ JonP, vâng, bạn chính xác. Bạn có thể kích hoạt bất kỳ sự kiện nào trong jquery bằng cách thay đổi 'popover'. 'popover' xuất phát từ thư viện bootstrap js. – yakutsa

1

Cố Bộ luật này:

<div id='a' style="border:2px solid black" > 
    <h3>Hove On me</h3> 
    For 2000 milliseconds. You will get an alert. 
    </br> 
    </div> 

$(document).ready(function(){ 
    var delay=2000, setTimeoutConst; 
    $('#a').hover(function(){ 
     setTimeoutConst = setTimeout(function(){ 
      /* Do Some Stuff*/ 
      alert('Thank You!'); 
     }, delay); 
    },function(){ 
     clearTimeout(setTimeoutConst); 
    }) 
}) 

</script> 

DEMO:

http://jsfiddle.net/uhwzzu1u/1/

2

Thanh toán mã dưới đây

var myVar; 
 
$("div#container") 
 
    .mouseover(function() { 
 
    myVar = setTimeout(function(){ alert("Hello"); }, 3000); 
 
    }) 
 
    .mouseout(function() { 
 
    clearTimeout(myVar); 
 
    });
div { 
 
    background: red; 
 
    margin: 20px; 
 
    height: 100px; 
 
    width: 100px; 
 
    display:block; 
 
    cursor: pointer; 
 
    } 
 
div:hover { 
 
    background: yellow; 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="container"></div>

+0

Tôi thích giải pháp này. Bạn có thể đính kèm điều này vào mỗi vùng chứa với một lớp cụ thể thay vì theo id không? –

+0

@SomaHoliday: Có tất nhiên bạn có thể sử dụng ** ClassName làm công cụ chọn thay vì ID ** .. chỉ sử dụng '$ (" div.ClassName ")' –

1

 
 
    var st; 
 
    $(".inner_pic").mouseenter(function(e) { 
 
     var that = this; 
 
     st = setTimeout(function() { 
 
      alert('testing'); 
 
     }, 3000); 
 
    }).mouseleave(function() { 
 
     clearTimeout(st);  
 
     alert('finish'); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="inner_pic"> 
 
    <h3>Picture Here - Hover me</h3> 
 
</div>

1

Giả sử bạn có một div với id của myelement, bạn có thể làm điều này:

var divMouseOver; 
$('#myelement').mouseover(function() { 
    divMouseOver = setTimeout(function() { 
    alert("3 seconds!"); //change this to your action 
    }, 3000); 
}); 
$('#myelement').mouseout(function() { 
    if (divMouseOver) { 
    clearTimeout(divMouseOver); 
    } 
}); 

BTW, đây là câu hỏi làm rõ hữu ích: sử dụng mouseentermouseover ngay tại đây: Jquery mouseenter() vs mouseover(). Hãy cân nhắc điều này khi chọn sử dụng.

+0

'divMouseDown' phải là' divMouseOver'. –

Các vấn đề liên quan