jQuery-检测鼠标是否仍然静止? [英] jQuery - detect if the mouse is still?

查看:84
本文介绍了jQuery-检测鼠标是否仍然静止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以检测jQuery中鼠标是否闲置了大约3秒钟.有我不知道的插件吗?因为我不相信会有本机的jQuery方法.任何帮助将不胜感激!

I was wondering if there was a way to detect if the mouse is idle for like 3 seconds in jQuery. Is there a plugin that I am unaware of? Because I don't believe there to be a native jQuery method. Any help would be much appreciated!

推荐答案

您可以侦听mousemove事件,在发生事件时启动超时,并取消任何现有的超时.

You can listen to the mousemove event, start a timeout whenever it occurs and cancel any existing timeout.

var timeout = null;

$(document).on('mousemove', function() {
    clearTimeout(timeout);

    timeout = setTimeout(function() {
        console.log('Mouse idle for 3 sec');
    }, 3000);
});

演示

这也可以很容易地在没有jQuery的情况下完成(仅绑定事件处理程序是特定于jQuery的).

This can be very easily done without jQuery as well (only binding the event handler here is jQuery-specific).

这篇关于jQuery-检测鼠标是否仍然静止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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