Im implementing a stopwatch in salesforce. It runs fine if Im in the same time where the stopwatch is ticking(lets say tab a) but if I switch to another tab(lets say tab b ) for a while the time is not accurate when I return to the TAB A. I think that if i implement a worker function as a static resource I might achieve what Im hoping for i.e the clock to work accurately even Im working in TAB B. I tried to implement worker class but the code fails when entered into the worker resource. Here is the code
stopwatch.js
start(event) {
var worker = new Worker("/resource/webworker");
worker.onmessage = function(e) {
timeVal = e.data;
console.log(e.data);
};
}
webworker.js
onmessage = function(e) {
totalMilliseconds = 0;
timeIntervalInstance;
// Run timer code in every 1000 milliseconds
this.timeIntervalInstance = setInterval(function() {
// Time calculations for hours, minutes, seconds and milliseconds
var hours = ( Array(2).join('0') + Math.floor((this.totalMilliseconds % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)) ).slice( -2 );
var minutes = ( Array(2).join('0') + Math.floor((this.totalMilliseconds % (1000 * 60 * 60)) / (1000 * 60)) ).slice( -2 );
var seconds = ( Array(2).join('0') + Math.floor((this.totalMilliseconds % (1000 * 60)) / 1000) ).slice( -2 );
// Output the result in the timeVal variable
timeVal = hours + ":" + minutes + ":" + seconds;
this.totalMilliseconds += 1000;
postMessage(timeVal);
}, 1000);
}
Im not good with javascript can anyone guide me what Im doing wrong here? Thank you.