jQuery - 3 minutos em segundos
Pergunta
Achei que seria fácil, mas parece que sou muito curto ou muito longo.
Estou tentando fazê -lo assinar o usuário depois de 3 minutos, este é a contagem que eu pensei que funcionaria que tentei 3000, 300, 3*60, 3*1000 e assim por diante
var timeout = 30*1800;
Esta é a função que estou tentando executar,
function loadidle(){
var timeout = 180000;
//alert(timeout);
$(document).bind("idle.idleTimer", function(){
logout();
});
$.idleTimer(timeout);
}
Solução
Você só precisa de um cronômetro simples.Existem muitas variedades.Aqui está um exemplo muito barato que o abstrai muito bem como uma classe.Você pode “continuar” o cronômetro chamando .reset().
function Timeout(seconds, callback){
this.length = seconds * 1000;
this.callback = callback;
this.start();
}
Timeout.prototype = {
start: function(){
var self = this;
this.stop();
this.timer = setTimeout(function(){
self.complete();
},this.length);
},
stop: function(){
if (this.timer) clearTimeout(this.timer);
this.timer = null;
},
complete: function(){
if (this.callback) this.callback();
this.stop();
},
reset: function() {
this.stop();
this.start();
}
}
Inicie um novo cronômetro:
var timer = new Timeout(3 * 60, logout);
timer.reset(); // refresh the timer
timer.stop(); // cancel the timer
Outras dicas
Tenho certeza de que JS (e, portanto, jQuery) usa milissegundos, então você vai querer 3*60*1000.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow