2011-02-11 64 views
1

我想最終得到此代碼工作:window.setTimeout不能正常工作

function Timer(callback, delay) { 
    var timerId, start, remaining = delay; 

    this.pause = function() { 
     window.clearTimeout(timerId); 
     remaining -= new Date() - start; 
    }; 

    this.resume = function() { 
     start = new Date(); 
     timerId = window.setTimeout(callback, remaining); 
    }; 

    this.resume(); 
} 

var timer; 

function onEvent(){ 
    timer = new Timer(anotherEvent(), 5000); 
} 

但是,這是行不通的,所以我簡化它,看看可能是什麼問題,並得到了它下降到:

var timer; 

function event(){ 
    timer = window.setTimeout(anotherEvent(), 5000); 
} 

並且它所做的全部是另一個事件()立即。

任何想法?

回答

7

通過anotherEvent作爲函數的參數。你現在擁有的是anotherEvent()其中調用的是的功能,並通過其返回值作爲參數。

+0

+1真是常見的錯誤。 – JCOC611 2011-02-11 21:36:58