鼠标在div上2秒钟时显示警告 [英] Show an alert when mouse is on a div for 2 seconds

查看:87
本文介绍了鼠标在div上2秒钟时显示警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面中确定 div ,我需要这样做:

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


当用户鼠标在 div 上站立2秒而没有移出时,则会显示警告

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.

推荐答案

将鼠标悬停在元素上时,请使用 setTimeout 请求一个警告框,延迟为2000毫秒(2秒)。当用户移动鼠标时,使用 clearTimeout setTimeout 重置计时器。

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.

示例,小提琴: http://jsfiddle.net/6SyLb/1/

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);
};

这篇关于鼠标在div上2秒钟时显示警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆