yy-loop
Simple and configurable game/main loop
features
- emits events (using eventemitter3)
- run every frame or after an interval
- control how many times each function runs
- loop pauses when app loses focus
- uses requestAnimationFrame()
rationale
This is a replacement for YY-Update. I use this in all my games, and as a base class for my other libraries, including pixi-ease and pixi-viewport.
installation
npm i yy-loop
simple example
const Loop = require('yy-loop')
const loop = new Loop()
// add a function to the loop that logs the elapsed time
loop.add((elapsed) => console.log(elapsed))
// add a function to the loop that runs ever second for 5 times
const entry = loop.add((elapsed) => console.log(elapsed), 1000, 5)
// run each time entry is called
entry.on('each', () => console.log('It has been one second.'))
// run after entry is complete
entry.on('done', () => console.log('completed'))
live example
https://davidfig.github.io/loop/
API
src/loop.js
constructor(options)
start()
stop()
update(elapsed)
interval(callback, time, count)
add(callback, time, count)
timeout(callback, time)
remove(entry)
removeAll()
get count()
get countRunning()
src/entry.js
class Entry extends Events
{
constructor(callback, time, count)
update(elapsed)
set pause(value)
License
MIT License
(c) 2017 YOPEY YOPEY LLC by David Figatner