System library

Note Content

System library provides functionality as system output, multithreading control and timers.


var system = require('system');

These functions provides text output to system console - standard output

println (text);

String text - print text to system console and append new line

print (text);

String text - print text to system console without new line

Example print() and println()

var system = require('system');
var text = "some text to output";
system.print("this is ");

Memory, System time

Number freemem();

Returns free memory amount in bytes.

Number totalmem ();

Returns total memory amount in bytes.

Number currentTimeInMillis ();

returns the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

Example memory operations, system time

var sys = require('system');
var totalMemory = sys.totalmem();
var freeMemory = sys.freemem();
var timeStamp = sys.currentTimeInMillis();
sys.println("Total memory:"+totalMemory+" free memory:"+freeMemory);
sys.println("Time in milliseconds:"+(sys.currentTimeInMillis()-timeStamp);


This functionality provides thread creating.

runAsThread(threadName, func, callback);

String threadName - name of thread.

function | Object funct - function funct will be launched in new thread with identification as threadName.

function callback - function or run method of Object will be invoked (from event loop) when function funct will be done.

sleep (delay);

Number delay - function does pause of program or thread for delay in milliseconds.

Warning Do not use sleep() in main thread (loop). It will block event loop.

Example with function

var sys = require('system');
var myfunc = function(){
    for (i=0;i<20;i++){
    return "done";

sys.runAsThread("mythread", myfunc,function(result){
    sys.println("Thread is "+result);

Example with Object's method .run()

var sys = require('system');
var Ticker = function(){
    this.interval = 1000; = function(){
        var count = 0;
        return "done";
//create ticker object
var ticker = new Ticker();
sys.runAsThread("mythread", ticker,function(result){
    sys.println("Thread is "+result);


Timeout is asynchronous timed function.

setTimeout (func, timeout);

Function func - Function will be once launch after timeout once time. Number timeout - timeout in milliseconds

setTimeout (func, timeout, repeats);

Function func - Function will be repeated with period defined by timeout.

Number timeout - Time in millisecond.

Number repeats - Number of repeats is defined by repeats. 0 = always.

Example timeout()

var sys = require('system');
//only one after 2 sec.
        sys.println("only one tik")


//repeat 5 times with 1 second period

results matching ""

    No results matching ""