Show an alert when mouse is on a div for 2 seconds

别说谁变了你拦得住时间么 提交于 2020-01-05 09:17:40

问题


I have certain div in the page and I need to do this:

When the user mouse stands on the div for 2 seconds without moving out, then an alert will show

I came here because I don't really know where to start, what to do, how to make it.

I searched on the web but I got no results. Any tutorial, resources, guide or example would be great.


回答1:


When hovering over the element, use setTimeout to request an alert box, with a delay of 2000 milliseconds (2 seconds). Reset the timer using clearTimeout and setTimeout when the user moves the mouse.

Example, Fiddle: http://jsfiddle.net/6SyLb/1/

var div = document.getElementById("thediv");
function alerter(){
    alert("Test")
    timer = setTimeout(alerter, 2000);
}
var timer;
div.onmousemove = function(){
    clearTimeout(timer);
    timer = setTimeout(alerter, 2000)
};
div.onmouseover= function(){
    clearTimeout(timer);
    timer = setTimeout(alerter, 2000)
}
div.onmouseout = function(){
    clearTimeout(timer);
};


来源:https://stackoverflow.com/questions/7786686/show-an-alert-when-mouse-is-on-a-div-for-2-seconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!