Sandip Bantawa
Sandip Bantawa

Reputation: 2880

Simple Node.js example not working in Windows 7

I am seeing some demos of node js

setInterval(function(){
    console.log('world');
}, 2000);

console.log('hello');

I have installed node js for Windows, this prints hello and then world when I run it from command prompt as per instruction at a regular interval but within that interval I am not able to do anything. Am I missing something in Windows 7

Upvotes: 0

Views: 126

Answers (3)

Metalskin
Metalskin

Reputation: 4268

I think your problem is understanding what is meant by "so we can do things". setInterval sets up a timer so that the specified function will be called every time the timer is up.

In the example given, the timer is every 2 seconds. This means that you can do something else in node.js while the timer is counting down. However in node.js there is only one thread so only one thing can execute at a time. The timer just gives the ability to say, when this time is up do something when nothing else is running.

So, for example, you could process network requests while the timer is counting down, you could read the contents of a file, you could parse some xml, etc.

I wrote code that uses a timer to check if a set of data was dirty. If it was then it would transmit the changes to all other clients. I had a listener on socket that would receive telemetry from a connected drone. If I cared about the telemetry then I would update the data. So I could update the data multiple times in a minute, while only check if I need to send an update every minute. So while the setInterval had set a repeating timer of every minute, when the function for the timer wasn't being executed, node.js could process any events from the listeners on the socket.

Note however, if your doing something during the time when the timer is up, then it node.js will have to wait until you finish before it can let the timer function execute.

Upvotes: 0

user1046334
user1046334

Reputation:

You misunderstood the words of the tutorial. When tutorial says "the process is on idle state rather than sleep so we can do things within that interval" it does not mean that the process runs in the background; it just means that the process itself can do a lot of things in those two seconds, unlike "sleep" command which will block and wait.

Running in the background is entirely different thing and is independent of the way how process itself works. It depends on the shell in which you are. In UNIX, you can run any process (no matter if it is non-blocking node or blocking wait or whatever else) by typing process arguments & (the & is important). But Windows command prompt does not have this (the nearest thing you can get is start node script.js, which runs it in new window and returns the prompt in previous window immediately.

IOW, it works exactly as expected.

Upvotes: 0

brandonscript
brandonscript

Reputation: 72885

I think you're misunderstanding what setInterval does. It's a blocking function that waits for the designated timeout (2000ms in your case) and then executes the contained function after the time expires.

Because Node.js is non-blocking (meaning it executes the entire code block in a single asynchronous stream), it will print hello before it prints the first world.

Explained:

setInterval(function(){
    console.log('world');
    // 2. This prints out after 2 seconds, and prints again every 2 seconds
}, 2000);

console.log('hello');
// 1. This prints out

In short, what you're seeing is the expected result.

Upvotes: 2

Related Questions