jQuery-在'x'分钟内未检测到任何操作并对该事件运行功能(X分钟内没有事件) [英] jQuery - detect no action for 'x' minutes and run function on that event (no event in X minutes)

查看:101
本文介绍了jQuery-在'x'分钟内未检测到任何操作并对该事件运行功能(X分钟内没有事件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测用户在"X"分钟内未执行任何操作并在此事件上运行功能?

How to detect that user have done nothing for 'X' minute and run function on this event?

E.G.

if( // no action from user for 'X' minutes) { //do stuff } ??

任何建议都值得赞赏.

推荐答案

在线演示

Live Demo

就像其他所有人都说过使用setTimeout一样,并将其设置为所需的等待时间.如果有任何活动,请使用clearTimeout清除超时.

Like everyone else has stated use setTimeout and set it to the desired amount of time to wait. If there is any activity just clear the timeout using clearTimeout.

// If theres no activity for 5 seconds do something
var activityTimeout = setTimeout(inActive, 5000);

function resetActive(){
    $(document.body).attr('class', 'active');
    clearTimeout(activityTimeout);
    activityTimeout = setTimeout(inActive, 5000);
}

// No activity do something.
function inActive(){
    $(document.body).attr('class', 'inactive');
}

// Check for mousemove, could add other events here such as checking for key presses ect.
$(document).bind('mousemove', function(){resetActive()});

这篇关于jQuery-在'x'分钟内未检测到任何操作并对该事件运行功能(X分钟内没有事件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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