0

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?

1
  • you may pass a fourth argument to scheduleFunction to be used as the argument to the passed function. Commented Apr 6, 2017 at 0:47

3 Answers 3

4

You can bind the arguments:

scheduleFunction(1000, 500, print.bind(null, "Hello World!"));

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();
}

function print(string) {
  console.log(string);
}

scheduleFunction(1000, 500, print.bind(null, "Hello World!"));

Sign up to request clarification or add additional context in comments.

Comments

2

Simple

scheduleFunction(1000, 500, function() {
  print("Hello World!")
});

1 Comment

Just enclosing a function with arguments inside another one with no arguments, isn't it?
0

When a function is called an array-like object variable called arguments is added to the function scope.

It holds the parameters that's were actually passed into the function and not just those defined in the object definition. That means that all parameters passed in can be accessed whether there be less or more passed in.

Knowing this, you could treat any parameters from the 4th argument onwards that are passed to scheduledFunction as the parameters that will be used to pass to functionName. This can be achieved by using Array#slice and Function#call (required because arguments is not an array but an array-like object):

var args =  [].slice.call(arguments, 4);

You can then use Function#apply to call functionName passing the sliced arguments as parameters:

functionName.apply(null, args);

The amended code would look something like this:

function scheduleFunction(t, deltaT, functionName) { 
  var timeout; 
  var timeoutID;

  // Get the rest of the parameters (if any)
  // from the 4th one onwards
  var args =  [].slice.call(arguments, 4);

  function scheduler() {
    // Call functionName, passing in all the extra parameters 
    functionName.apply(null, args);

    // Etc...
  }
  scheduler();
}

Use it like so, notice that print is no longer invoked, but the arguments passed to it just follow it as parameters to scheduleFunction:

function print(string) { 
  console.log(string);
}

scheduleFunction(1000, 500, print, "Hello World!");

You could print multiple arguments by modifying the way that console.log is called:

function print() {
  // No parameters defined BUT arguments is
  // still available.

  // Obtain a real array from it using slice
  var args = [].slice.call(arguments, 0);

  // Use Function#apply to call console log with
  // multiple parameters from args array
  console.log.apply(null, args);
}
scheduleFunction(1000, 500, print, "Hello World!, "Hello again!");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.