I am trying to build a kind of scheduler function, able to call another function with parameters at random instants in time. Here is my attempt, using Javascript:
function scheduleFunction(t, deltaT, functionName) {
var timeout;
var timeoutID;
function scheduler() {
functionName()
clearTimeout(timeoutID);
timeout = Math.trunc(Math.random() * 2 * deltaT - deltaT) + t;
timeoutID = setTimeout(scheduler, timeout);
}
scheduler();
}
This function works properly if I let it call another function that doesn't require parameters. For instance:
function printSomething() {
console.log("Printing something...");
}
scheduleFunction(1000, 500, printSomething);
Unfortunately, that function doesn't allow to call another function with parameter, that is - for example:
function print(string) {
console.log(string);
}
scheduleFunction(1000, 500, print("Hello World!"));
How should I edit the scheduler function in order to obtain that kind of result, if possible?