game-server-loop
Simple game loop that allow queue of sync instructions.
Installation
npm install ml-game-loop --save
Usage
'use strict'; var GameLoop = require('ml-game-loop'); var loop = new GameLoop(), i = 0; // this will run every time loop is started// every time will have counter instead writing new lineloop.add(function (time, diff) { // time is current timestamp with milliseconds // diff is a time that passed since last execution process.stdout.write("\r every time!" + String(i++));}); // this will run every after at least 100ms pass but always after previous callbackloop.throttle(100, function (time, diff) { process.stdout.write("\n* EVERY 100 MS !!!!!! *\n");}); // as above but after 1000ms and it is named "every-second"loop.throttle(1000, function (time, diff) { process.stdout.write("\n*** ALWAYS AFTER 1 SECOND - 1000 MS !!!!!! ***\n");}, 'every-second'); /* * start loop, remember that callback always run in same order so if all three callbacks are valid (and all are on first start) * you will see three sentences * - every time! X * - * EVERY 100 MS !!!!!! * * - *** ALWAYS AFTER 1 SECOND - 1000 MS !!!!!! *** * ALWAYS in same order */loop.start(); setTimeout(function () { // this will pause the loop after 5 seconds loop.pause(); // this will stop execution of "every-second" callback, use loop.enable('every-second') to resume it loop.disable('every-second');}, 5000); setTimeout(function () { // this will pause the loop after 7 seconds, 2 seconds after pause loop.resume();}, 7000); setTimeout(function () { // this will stop the program after 10 seconds of working loop.stop();}, 10000);