System library
Note Content
System library provides functionality as system output, multithreading control and timers.
Initialize:
var system = require('system');
Print text to the system console
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 ");
system.println(text);
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);
Multithreading
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++){
sys.println("tick");
sys.sleep(1000);
}
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;
this.run = function(){
var count = 0;
while(count<30){
sys.println("tick");
sys.sleep(this.interval);
count++;
}
return "done";
}
}
//create ticker object
var ticker = new Ticker();
sys.runAsThread("mythread", ticker,function(result){
sys.println("Thread is "+result);
});
Timers
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.setTimeout(function(){
sys.println("only one tik")
},2000);
//forever
sys.setTimeout(function(){
sys.println("tik2")
},2000,0);
//repeat 5 times with 1 second period
sys.setTimeout(function(){
sys.println("tik2")
},1000,5);