如果用户在过去5分钟内闲置在网页上,提示用户的最佳方法是什么? 我的意思是如何知道在过去5分钟内网页上的使用是空闲的。
答案 0 :(得分:7)
只需使用计时器设置一些事件侦听器即可触发自定义函数/事件。我建议尽可能多地绑定用户驱动的事件。
我要作弊并使用jQuery作为一些演示代码,这可以在纯javascript中完成,但这是jQuery存在的东西:
$(window).bind( 'mousemove mouseclick keydown mousewheel ...more listeners...', idle );
function idle( e )
{
if ( window.idleTimeout )
{
clearTimeout( window.idleTimeout );
}
window.idleTimeout = setTimeout( userIdle, 300000 );
}
function userIdle()
{
//either do stuff here or trigger the "idle" event
$(window).trigger('idle');
}
答案 1 :(得分:1)
使用setTimeout
函数调用以5分钟的延迟注销用户的函数。