jQuery - 3 minutes in seconds
문제
I thought this would be easy, but seems i either to way to short or way to long.
I am trying to make it sign the user out after 3minutes this is the count down I thought would work I have tried 3000, 300, 3*60, 3*1000 and so on
var timeout = 30*1800;
This is the function I am trying to run,
function loadidle(){
var timeout = 180000;
//alert(timeout);
$(document).bind("idle.idleTimer", function(){
logout();
});
$.idleTimer(timeout);
}
해결책
You just need a simple timer. There are many varieties. Here's a dirt cheap example that abstracts it nicely as a class. You can "continue" the timer by calling .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();
}
}
Start a new timer:
var timer = new Timeout(3 * 60, logout);
timer.reset(); // refresh the timer
timer.stop(); // cancel the timer
다른 팁
Pretty sure JS (and therefore jQuery) use milliseconds, so you'll be wanting 3*60*1000.
제휴하지 않습니다 StackOverflow