Allows the timer to jump backward due to network latency when restarting or continue playing a paused timer.
intervalID
Integer
The interval ID (internal).
intervalTime
Integer
10, 100, 1000
Interval time in [ms].
offsetToServerTime
Integer
The time offset of a local browser to Singular server time.
roundToInterval
Boolean
true (default)
Rounds time to the next interval.
tc
Object
{
"UTC": 0,
"isRunning": false,
"value": 0
}
The time control object.
UTC: UTC start time [ms];
isRunning: boolean;
value: offset in [ms]
timeoutID
Integer
The timeout ID (internal).
updateCallback
Function
A callback function triggered at every interval.
Methods
getCurrentTime()
Returns the current running time of the timer in [ms].
// This code prints the current running time to the consoleconstcurrTime=timeControl.getCurrentTime();console.log("currTime =", currTime);
Return
integer: time in [ms]
currTime=1234
setAllowBackwardsJump()
Due to a slow internet connection and/or network latency, clocks can jump backwards when starting, pausing, or restarting. Use the setAllowBackwardsJump() call to set this behavior.
// This code avoids backwards jumpstimeControl.allowBackwardsJump(false);
Return
none
setIntervalTime()
Sets the interval frequency for the timer.
Parameter
Name
Type
Description
tInterval
Number
The interval duration in [ms].
// This code sets the interval frequency to 1000 [ms]consttInterval=1000;timeControl.setIntervalTime(tInterval );
Return
none
setOffsetToServerTime()
Sets the offset from the local browser to the Singular server time.
Parameter
Name
Type
Description
tOffset
Number
Offset from the local browser time to the Singular server time.
// This code sets the offset to the Singular server time in [ms]consttOffset=876;timeControl.setOffsetToServerTime(tOffset);
Return
none
setRoundToInterval()
Rounds the time to the interval frequency.
Parameter
Name
Type
Description
doRounding
Boolean
true: (default) round time to interval frequency
false: do not round time
// This code sets the round-to-interval flagtimeControl.setRoundToInterval(true);
Return
none
setTimeControl()
The time control object defines the current timer state.
// This code shows examples of the time control objectconsttStart=Date.now();consttcStart= {"UTC": tStart,// set start time to current UTC time in [ms]"isRunning":true,// start timer"value":0// offset is 0};// .... laterconsttPause=Date.now();consttcPause= {"UTC": tPause,// set start time to current UTC time in [ms]"isRunning":false,// start false"value": tcPause - tcStart // offset is 0};// .... even laterconsttcContinue= {"UTC":Date.now(),// set start time to current UTC time in [ms]"isRunning":true,// start false"value": tcPause - tcStart // offset is 0};
Parameter
Name
Type
Description
tc
object
The time control object.
// This code starts the timertimeControl.setTimeControl(tcStart)(;
Return
none
setUpdateCallback()
Sets the time control callback function. This function is called at every interval.
Parameter
Name
Type
Description
cb
function
The callback function.
// The code below updates the timer callback functiontimeControl.setUpdateCallback(() => {console.log("elapsed time = %d[ms]",timeControl.getCurrentTime());// add your code here ...});
Return
none
let tControl = { UTC:0, isRunning:false, value: value};// The code below creates a timecontrol objectconsttimeControl=SingularWidget.createTimeControl();// set update callback functiontimeControl.setUpdateCallback(updateCallback);// this will stop the clock from jumping backwards when it is stopped. A jump// backwards can happen due to network latencytimeControl.setAllowBackwardsJump(false);// round the time to the interval frequencytimeControl.setRoundToInterval(true);// tell the timer control object how often to update the callbacktimeControl.setIntervalTime(100); //millisecondstimeControl.setTimeControl(tControl); // todo...constupdateCallback= () => {// get current timer valuelet currentTime =timeControl.getCurrentTime();console.log("currentTime = %d[ms]", currentTime);}