2012-07-17 29 views
17

只要好奇是否有辦法在underscore.js庫的_.delay函數中殺掉定時器。它在註釋源中使用setTimeout(),但我無法弄清楚實際做法。_.delay() - 查殺定時器

一個例子是:

_.delay(this.functionName, 5000) 

如果沒有被調用爲3秒,我想停止functionName被調用,我可以提前終止計時器?

回答

41
var timerId = _.delay(this.functionName, 5000); //save the timerid in a variable 
clearTimeout(timerId); //Kill the timer 
+1

非常快速,效果很棒。謝謝! – gleddy 2012-07-17 19:07:25

+0

很酷。讓我從進入下劃線和挖掘周圍。 :-) – jmk2142 2015-07-06 01:15:08