
Originally Posted by
poopoorino
Using the clientless NodeJs, how do you loop/update, like some sort of tick system?
And how is it possible to store data, perhaps into local json files or local sql databases?
Im kinda noob, so try not to over generalize since Im new to this :/
Thanks
If you want to run an event loop on a custom timer you can use the `setInterval` method which is built into Node.
Code:
setInterval(() => {
console.log('tick');
}, 1000 / 60);
The first argument is the function that will get called when a tick occurs, in this case it will just log 'tick' over and over again.
The second argument (1000 /60) is the number of milliseconds between the ticks. 1000 milliseconds is 1 second, so dividing by 60 will mean that the loop will fire 60 times per second.
If at some point you want to stop the loop from running, you can use the `clearInterval` method.
Code:
let ticks: number = 0;
const loop = setInterval(() => {
ticks++;
if (ticks > 100) {
clearInterval(loop);
console.log('100 ticks have passed.');
}
}, 1000 / 60);
As for storage, there are many npm modules which are available for storing data in a database. I use Firebase for a lot of projects since it uses JSON as the storage format, so you can pretty much interact directly with the data in a JavaScript program without transforming it first.
Storing data locally with JSON is also pretty easy. Have a look at the Node
File System module docs (fs.writeFile is probably what you want to look at).