2016-12-09 43 views
3

我正在使用V8與C++和本地窗口setInterval相結合functon未定義。如何定義setInterval函數

什麼是algaorythm創建類似本地setInterval,但在純js?

+0

由於JavaScript的本身並沒有提供一種方式來任務添加到事件隊列,沒有什麼可以用「純粹」的JavaScript做。也許看看V8 API? –

+0

嘗試查看nodejs如何實現定時器,或許可以幫助您。 –

+0

[在純JavaScript中實現setTimeout()和setInterval()]的可能的重複](http://stackoverflow.com/questions/35824722/implementing-settimeout-and-setinterval-in-pure-javascript)。另請參見[如何在Node.js中解釋和執行異步JavaScript?](http://stackoverflow.com/questions/36491385/how-is-asynchronous-javascript-interpreted-and-executed-in-node-js) – guest271314

回答

1

假設setTimeout是可用的(不是可能的,但你沒有指定):

function setInterval(fn, t) { 
    let id = {}; 

    function wrapper() { 
    id.timeout = setTimeout(wrapper, t); 
    fn.apply(this, arguments); 
    } 

    id.timeout = setTimeout(wrapper, t); 

    return id; 
} 

function clearInterval(id) { 
    clearTimeout(id.timeout); 
} 
+0

'setTimeout'沒有定義,但謝謝你的嘗試 –