我想在用戶空閒超時5秒時顯示優惠橫幅,但下面的代碼顯示優惠橫幅每空閒5秒。但是我想只對第一個空閒執行以下功能一次5 seconds.Any身體幫我出只對用戶空閒超時執行一次功能
$(document).ready(function(){
idleTime = 0;
//Increment the idle time counter every five second.
var idleInterval = setInterval(timerIncrement, 5000);
function timerIncrement()
{
console.log(idleTime++);
if (idleTime > 5)
{
doPreload();
}
}
//Zero the idle timer on mouse movement.
$(this).mousemove(function(e){
idleTime = 0;
});
function doPreload()
{
$('#add-click').click();
}
});
移動的,如果(空閒...)裏面的doPreload ..我認爲你不能檢查計時器內的idleTime變量,因爲封閉。 – Max