Page tree
Skip to end of metadata
Go to start of metadata

JavaScript is a single threaded environment. However, it is possible to run function blocks with timers without blocking the current flow of execution. Those timers operate similar to threading in programming.

In the Smartface, it is possible to run a code in a separate thread using the same methods in Web development: setTimeout and setInterval.

setTimeout

This function calls the parameter function only once after the initial duration.

setInterval

This function calls the parameter function repeating in the given period.

Function Signature

setTimeout(func, milliseconds);
setInterval(func, milliseconds);

Parameter Values 

function:

Required. The function that will be executed.

milliseconds:

Required. The time duration in milliseconds to wait before executing the code, i.e. for setTimeout function, or the period of execution, i.e. for setInterval function.

Return Value

A number, integer value of the ID or the job. This value is used while cancelling the job.

Cancelling

Cancelling of a job can be done by using one of the two functions (clearTimeout or clearInterval). Those two functions do the same work, "clear / cancel" the job with the given ID.

clearTimeout can cancel the job of setInterval. clearInterval can cancel the job of setTimeout or vice versa.

Examples

Timeout

var timeoutID = setTimeout (function () {
        setHello();
        }, 3000);
function setHello() {
        alert("Hello");
}
function cancelHello() {
       clearTimeout(timeoutID);
}

Interval

var intervalID = setInterval(function () {
		updateTime();
	}, 1000);
function updateTime() {
	var d = new Date();
	var t = d.toLocaleTimeString();
	Pages.Page1.Label1.text = t;
}
function stopTimer() {
	clearInterval(intervalID);
}