2014-01-14 35 views
1
function TimerSystem() { 
    this.timer = null; 

    this.addTimer = function(timer, delay) { 
     this.timer = timer; 

     setTimeout(function() { 
      timerSystem.timer = null; 
     }, delay); 
    }; 
} 
var timerSystem = new TimerSystem(); 


timerSystem.addTimer(setTimeout(function() { 
      //do something... 
     }, 1000), 1000); 

它在我的源代碼中工作。 讓我知道這種方式是對的? 我想,這是不是智能javascript check setTimeout正在運行

回答

0

記住timeoutId,並將其設置爲空當計時器FUNC符文,這樣的:

function schedule(func, millsecs) { 

    var timeId = setTimeout(function() { 

     //the function runed, clear this timeId 
     timeId = null; 

     func(); 

    }, millsecs); 

    return { 

     isrunning : function() { 

      return timeId != null; 
     } 
    }; 
} 

var sinfo = schedule(function() { 

    console.log('done'); 

}, 500); 

console.log('just now', sinfo.isrunning()); 

http://jsfiddle.net/rooseve/AVna6/1/

此外,你爲什麼需要「檢查的setTimeout在跑」?

+1

我想在定時器運行時阻止用戶輸入。 謝謝! :) – user2135500