Create file main.js, code as follows: node.js
function printHello(){ console.log( “Hello, World!” ); Var t = setTimeout(printHello, 2000);
// Clear timer clearTimeout(t); Execute the main.js file as follows:
$node main.js setInterval(cb, ms) setInterval(cb, ms) The global function executes the specified function (cb) after the specified number of milliseconds (ms).
Returns a handle value representing the timer. You can use the clearInterval(t) function to clear timers.
The setInterval() method keeps calling the function until clearInterval() is called or the window is closed.
The instance creation file main.js looks like this:
function printHello(){ console.log( “Hello, World!” ); } setInterval(printHello, 2000); Execute the main.js file as follows:
$ node main.jsHello, World! Hello, World! Hello, World! Hello, World! Hello, World! … The program prints “Hello, World!” every two seconds. “And will continue forever until you press CTRL + C.
Console Console is used to provide console standard output. It is a debugging tool provided by Internet Explorer’s JScript engine and has gradually become an implementation standard for browsers.
Node.js follows this standard, providing console objects that conform to custom behavior and output characters to either the standard output stream (STdout) or the standard Error stream (STderr).
Console methods The following are methods of the console object:
1 console.log([data][,…] ) prints characters to the standard output stream and ends with a newline character. This method takes several arguments, and if there is only one argument, prints the argument as a string. If there are more than one argument, it is printed in a format similar to the C printf() command. 2 console.info([data][, …] ) This command returns an informational message and is not much different from console.log, except that it only outputs text in Chrome, with a blue exclamation mark. 3 console.error([data][, …] Output error message. The console displays red forks when errors occur. 4 console.warn([data][, …] ) output warning messages. A yellow exclamation mark appears on the console. 5 console.dir(obj[, options]) inspects an object and displays it in a format that is easy to read and print. 6 console.time(label) Output time, indicating that the timing starts. 7 console.timeEnd(label) End time. 8 console.trace(message[, …] This test function is useful to run, just add console.trace to the function you want to test. 9 console.assert(value[, message][, …] ) is used to determine whether an expression or variable is true and takes two arguments, the first an expression and the second a string. The second argument is printed only if the first argument is false, otherwise there is no result. Console.log () : Prints characters to the standard output stream and ends with a newline character. Console. log receives several arguments, and if there is only one argument, outputs the argument as a string. If there are more than one argument, it is printed in a format similar to the C printf() command.
The first argument is a string. If there is no argument, just a newline is printed.
console.log(‘Hello world’); console.log(‘byvoid%diovyb’); console.log(‘byvoid%diovyb’, 1991); The running results are as follows:
Hello world byvoid%diovyb byvoid1991iovyb console.error() : The same as console.log(), but output to the standard error stream. Console.trace () : outputs the current call stack to the standard error stream. console.trace(); The running results are as follows:
Trace: at Object. (/home/byvoid/consoletrace.js:1:71) at Module._compile (module.js:441:26) at Object.. js (module.js:459:10) at Module.load (module.js:348:31) at Function._load (module.js:308:12) at Array.0 (module.js:479:10) at EventEmitter._tickCallback (Node.js :192:40)
Console. info(” Program starts executing: “);
var counter = 10; Console. log(” count: %d”, counter);
Console. time(” Get data “); // // Do some code // console.timeEnd(‘ get data ‘);
Console. info(” Program completed.” ) execute the main.js file as follows:
$node main.js program starts execution: count: 10 Get data: 0ms program finishes execution Process Process is a global variable, i.e. a property of the global object.
It is an object that describes the current node.js process state and provides a simple interface to the operating system. You’ll usually have to deal with it when you’re writing a native command line program. Some of the most common member methods of the Process object are described below.
No. Event & Description 1 Exit Triggered when the process is about to exit. 2 beforeExit Fires this event when the node clears the event loop and has no other arrangements. Normally, Node exits when no process is scheduled, but the ‘beforeExit’ listener can be called asynchronously so node will continue executing. 3 uncaughtException When an exception bubbles back into the event loop, the event is triggered. If you add a monitor to the exception, the default action (print the stack trace and exit) does not occur. 4 Signal Event Triggered when a process receives a Signal. For the list of signals, see the standard POSIX signal names, such as SIGINT and SIGUSR1. The instance creation file main.js looks like this:
process.on(‘exit’, function(code) {
// The following code will never execute setTimeout(function() {console.log(” this code will not execute “); }, 0);
Console. log(‘ exit code :’, code); }); Console. log(” program execution ends “); Execute the main.js file as follows:
The exit code of $node main.js program is 0