2011-09-12 214 views
0

我认为这很容易,但看起来我要么想办法缩短,要么延长。jQuery - 几秒钟内完成3分钟

我试图使它签署用户3分钟 出来后,这是倒计数我想会的工作我都试过3000,300,3 * 60,3 * 1000等

var timeout = 30*1800; 

这是我试图运行的功能,

function loadidle(){ 

      var timeout = 180000; 
      //alert(timeout); 

      $(document).bind("idle.idleTimer", function(){ 

       logout(); 

      }); 


      $.idleTimer(timeout); 
} 
+0

1000 * 60 * 3(1000毫秒为单位,时间60秒,时间3分钟) – galchen

回答

1

你只需要一个简单的计时器。品种很多。这是一个很便宜的例子,它很好地将它抽象为一个类。您可以通过调用.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(); 
    } 
} 

开始一个新的计时器:

var timer = new Timeout(3 * 60, logout); 
timer.reset(); // refresh the timer 
timer.stop(); // cancel the timer 
0

很确定的JS(因此jQuery)使用毫秒,所以你会想要3 * 60 * 1000。

+0

,而我在这,用 '秒' 和“分'标签并不是最好的实践! –