Please implement an efficient setIntervals function in JavaScript / C# using a
single system core function called setSystemTimer
The function signatures:
setSystemTimer(duration: number, cb: function): void - Calling
setSystemTimer before it returns will cause an exception to be thrown ErrorBusy
setIntervals(durations: number[N], cbs: function[N]) setIntervals will be
called once in the beginning of the application and will set up a set of N periodic
timers such as timer T1 will trigger every durations[0] ms and will call cbs[0],
timer T2 trigger every durations[1] ms and will call cbs[1] and so on.
setIntervals(durations: number[N], cbs: function[N])
Example
const start = Date.now();
function cb0 =
() => console.log('Timer 1: ', Date.now() - start);
function cb1 =
() => console.log('Timer 2: ', Date.now() - start);
function cb2 =
() => console.log('Timer 3: ', Date.now() - start);
const cbs = [cb0, cb1, cb2];
const durations = [3,5,7];
setIntervals(durations, cbs);
// (3s) Timer 1: 3000
// (5s) Timer 2: 5000
// (6s) Timer 1: 6000
// (7s) Timer 3: 7000
....
// (15s) Timer 1: 15000
// Timer 2: 15000
setSystemTimer(duration: number, cb: function): void
setSystemTimer(3000, )
main() {
const cbs = [cb0, cb1, cb2];
const durations = [3,5,7];
setIntervals(durations, cbs);
// (3s) Timer 1: 3000
// (5s) Timer 2: 5000
// (6s) Timer 1: 6000
// (7s) Timer 3: 7000
....
// (15s) Timer 1: 15000
// Timer 2: 15000
}