using setInterval in javaScript

Many javaScript projects will require some kind of main application loop that will execute over an over again. There are many ways to go about doing this, one of which is the setInteval method. It is not always the best option for doing so, but depending on the nature of the project sometimes it might be what is called for to get an app loop up and running.

The setInterval method will fire a given method after a given millisecond value has elapsed, after which point it will fire again after the given millisecond value has elapsed again, and so on. It is therefor a popular, and well known way of implementing an application loop. It can also be thought of as a starting point that can branch off into other topics such as state management, and the nature of how threading, and event loops in a javaScript environment. However maybe it would be best to look into other options on top of setInterval before getting into any of that.

So then there are alternatives to setInterval to be aware of such as setTimeout, and requestAnimationFrame when it comes to client side javaScript. The setTimeout method works the same way more or less as setInterval, but will just fire the given method once after a delay. However the setTimout method can be called from within the body of the method that is begin called that can result in a similar effect to the use of setInterval. In addition to setTimeout the requestAnimationFrame is yet another options to be aware when it comes to client side javaScript that might prove to be a better choice when it comes to making canvas projects.

There is also the topic of threading that often comes up when talking about setInterval, and similar methods when working with what is called an event loop, and ways to have more than one event loop. I see lots of javaScript developers saying that javaScript is a single threaded language, I shy away from saying that because it strikes me as a bit of a half truth actually. In a modern web browser there are ways of sining up more that one event loop, which does result in more than one independent thread, but on a per process basis. So in a way it is true that javaScript is a single threaded language compared to what may be possible with other languages, but it is important to know what you mean by that.

The subject of what is often refereed to as true threading is a complex topic that is something that is outside the scope of this post, it is not something that can be done with setInterval by itself at least, and possible not with javaScript at all depending on how you go about labeling what true threading is. So in this post I will just be sticking to some basic examples of setInterval, and will not be getting into what can be done with things like webWorker in a client side javaScript environment, and the child process module in nodejs.

Read More

Using mathjs as a more advanced alternative the the Core JavaScript Math Object

When it comes to doing anything with math in javaScript there is of course the core javaScript Math object that is very helpful. The Object is packed with a whole bunch of usual suspects when it comes to all kinds of things that have to do with Math such as sin, cosine, a PI constant, and so forth. However the Native Math object does have it’s limitations, and does not always work the way I would like it to. I find myself often having ti create a few stand alone methods sometimes such as a nth root method, and also look around for something to help with big number support. There are also many methods missing that have to do with things that have to do with statistics when it comes to things like the various kinds of means, and standard deviation.

A popular alternative to the corejs Math object is math.js, which can be found on npm, and github like most projects. The mathjs module can do everything the Math object can, however it also adds a whole bunch of new features such as big number support. When it comes to native javaScript there is now a native Big Number standard, but it still might now work great in all browsers. In addition certain mathjs equivalents to the Math object methods have additional features, and also a plug in system exists that can be used to further extend mathjs.

Read More

An enhanced child_process for node.js called execa

I have written a post on using the built in node.js child_process module which is one way to go about launching additional processes in a node.js environment. The module is a great built in starting point for starting an external command in the host operating system in which nodejs is running. The built in module seems to work just fine for the most part, however there might come situations in which I might need to use something that builds on top of this core built in nodejs module.

So far I have not ever really looked into, and tested out something that is an improvement over the built in child process module. However today I have come across something called execa that does the same thing as child_process, but adds some more features, such as making each method a promise.

Read More

Turning a node.js method into a promise with pify

When making a node.js project, many methods in the node.js core work by giving a callback that will return an error, or what it is that you want from the method. This is a callback style method that can result in the so called callback hell when it comes to doing anything where many of these kinds of calls need to be nested.

In late versions of nodejs many core modules now return a promise as an alternative to this cllback style way of doing things. Also there is the promisify method in the util module that I often use as a way to promsify these built in methods. That solution will also work on most older versions of nodejs, at least all the one that are still supported anyway.

Another option would be to just make a quick method where I am returning a new instance of a promise constructor when it comes to any version of node that supports Promises, or failing that, by using a user space module like bluebird to add promises. However this is a post on the npm package pify, one of many project created and maintained by sindresorhus, it is a nice little project that can help to make quick work of this also so lets look at some quick examples.

Read More

Using scatter plots to gain a better understanding of statistics

The subject of Statistics can become a little complicated, but if you are like me visualizing what is going on can help to make something complicated easier to understand. In this post I will be using scatter plots to help gain a better understanding of certain subjects in statistics.

A scatter plot is a good way to go about visualizing data where each data element in a population or sample has at least two values that can be mapped to the x and y axis. It can also be used as a way to make sense of things like the differences between the various means, and all kinds of various values that can come up in such a graph.

I have made lots of projects that involve having a lot of display objects that are placed in various locations within a canvas matrix. This is a common tasks when it comes to making a wide rang of games after all, so having a scatter plot is not to dissimilar to that of having an object pool. The only difference then is applying some meaning to the position of the objects, rather than having them just randomly move around the canvas like I usually do.

Read More