2016-10-04 41 views
0

我试图找出如果我可以设置时间出去点击按钮,而不是使用闲置的东西是这里介绍:Fire Event When User is IdlejQuery的,如果类没有被点击时x分钟做

我会把它喜欢相关的链接类, 因此,例如,如果用户没有点击链接,过了2分钟:

jQuery的

if (".linkClass").(idleTime > 2) { 
    alert("Hurry Up!"); 
    } 

有什么建议?

+2

使用'setTimeout()'。点击按钮后,清除超时并重新启动。当超时执行时,显示你的'快点'提醒' –

+0

其实是的......哇,这是如此简单,TNX! :) – DearBee

回答

3
function impatientButton(el, wait) { 
    var timeout = 0; 

    setTimeout(function(){ 
    alert('Hurry up!'); 
    }, wait); 

    $(el).click(function(){ 
    clearTimeout(timeout) 
    }); 
} 

// Initialise 
impatientButton('.selector', 10000); 

像这样的事情应该做的伎俩!这将设置一个计时器以在10秒后触发警报。如果在此之前点击该按钮,定时器将被清除。

1
var clickTimeOut=null; 

$(".linkClass").on("click",function(){ 
    clickTimeOut = setTimeout(alertTimeOut, 1000*120); 
}); 

function alertTimeOut() 
{ 
    alert("Hurry Up!"); 
} 
1

计时器是你在找什么... 也许是这样的:

var idleTimerId; 
$('.linkClass').on('click', function(event){ 
    idleTimerId = setTimeout(function(){ alert("Hello"); }, 5000); 
}); 

// in this example the timer will be initialized when the link is clicked first. 
// Then waits 5000ms to fire. if you want to cancel it - call clearTimeout() 
// with idealTimerId; 

更多关于JavaScript的定时器的相关信息这个优秀的文章:

http://ejohn.org/blog/how-javascript-timers-work/

http://www.w3schools.com/jsref/met_win_settimeout.asp

1
var mt; 

function start_timer() { 
    mt = setTimeout(function() { 
     alert("Hurry Up!"); 
    }, 12000); 
} 

function reset_timer() { 
    clearTimeout(mt); 
    start_timer(); 
} 

$('.linkClass').click(function() { 
    reset_timer(); 
}); 

start_timer();