For games, the built-in timers are not suitable, mainly because of the following two reasons:
This timer does not maintain it's own clock, so you need to call `update()` in your render loop.
Include the timer.js file via script tag or pull it in using an AMD loader like require.js.
var timer = new Timer();
// setting an interval works like window.setInterval:
var intervalId = timer.setInterval(someFunc, 100);
// to set the execution context, pass a context object:
var intervalId = timer.setInterval(someFunc, 100, someObj);
// or bind your function:
var intervalId = timer.setInterval(someFunc.bind(somObj), 100);
// then, in your render loop:
function render(){
//...
timer.update(delta); // delta needs to be in ms!
}
// clearing also works as expected:
timer.clearInterval(intervalId);
You can see how it works here.
You can run the tests here.
Ping me on Twitter: @jensarps