✨ Basic timer
Description
A basic timer function which uses PIXI.ticker.Ticker() to start an update loop and invoke a function after a set amount of time.
Why is this change important
This is useful to help avoid using the much slower setTimeout
It should also save time by helping you avoid not creating your own timer method every time you need one.
Possible Implementation
/**
* Similar to a setTimeout this instead uses PIXI.ticker to invoke a function
* after a set amount of time.
*
* @param {*} time The amount of time (ms) for this timer to wait
* @param {*} callback The callback function to invoke on timer completion
*/
export function Timer (time, callback) {
const ticker = new PIXI.ticker.Ticker()
ticker.start()
let elapsed = 0
const update = (delta) => {
elapsed += ticker.elapsedMS
if (elapsed >= time) {
if (callback && typeof callback === 'function') {
callback()
ticker.remove(update)
}
}
}
ticker.add(update)
}