所以我试图使用计时器间隔运行一个弹出脚本,并且无论出于什么原因,计时器不计数,它只会在加载时触发。
idleTime = 0;
$(document).ready(function() {
$limit = 5;
if ($.cookie('newVisit') != '1') {
$.get('/pop_form.htm', function(data) {
$('.subs-popup').html(data);
});
function timerIncrement() {
idleTime++;
if (idleTime > $limit) {
$('.subs-popup ').show();
idleTime = 0;
}
}
var idleInterval = setInterval(timerIncrement, 1000); // 1 second
$(this).mousemove(function(e) {
idleTime = 0;
});
$(this).keypress(function(e) {
idleTime = 0;
});
$.cookie('newVisit', '1', {
expires: 364,
path: "/"
});
}
});
只是不太确定要做什么才能使它正常工作吗?任何帮助将不胜感激。
更新***
所以我按照您的意思做了,这是更改后的代码。除了现在,我收到了意外的令牌错误。
var idleTime = 0;
$(document).ready(function() {
var limit = 7;
if ($.cookie('newVisit') !='1') {
$.get('/pop_form.htm', function(data) {
$('.subs-popup').html(data);
});
function timerIncrement() {
idleTime++;
if (idleTime > limit) {
$('.subs-popup ').show();
idleTime = 0;
}
}
var idleInterval = setInterval(timerIncrement, 1000); // 1 second
$(this).mousemove(function (e) {
idleTime = 0;
});
$(this).keypress(function (e) {
idleTime = 0;
});
$.cookie('newVisit', '1', { expires: 364 , path: "/" });
}
}); <----- this is where i get the unexpected token error
这是一个替代解决方案:
var timeout = null;
function resetTimer() {
clearTimeout(timeout)
timeout = setTimeout(function() {
clearTimeout(timeout)
alert("Idle!")
}, 5000);
}
$(document).mousemove(function(e) {
resetTimer();
});
$(document).keypress(function(e) {
resetTimer()
});
这将使用返回值setTimeout()
作为超时参考。您可以clearTimeout()
使用此参考进行调用,它将取消/清除/删除超时,因此您每次鼠标移动/按下一个键时都可以启动一个新超时-而不是记录它被击中了多少次。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句