JavaScript setInterval function may or may not have parameters -
i working on simple higher-order function, delay
, invoke function parameter, func
, , delay amount of time passed in wait
. have read other answers put in parameters, none of answers found addressed need learn: , how should allow parameters may or may not passed func
?
original instructions: "invokes func after wait milliseconds. additional arguments provided func when invoked."
here's basic start:
function delay(func, wait) { setinterval(func, wait); }
another answer on states anonymous function can used wrap func
parameter parameters can passed in there, haven't had success building yet.
guidance appreciated.
it sounds need make use of arguments
pseudo-array, , function#apply
:
function delay(func, wait) { // arguments after first 2 var args = array.prototype.slice.call(arguments, 2); settimeout(function() { func.apply(null, args); }, wait); }
example:
function delay(func, wait) { var args = array.prototype.slice.call(arguments, 2); settimeout(function() { func.apply(null, args); }, wait); } function outputperson(firstname, lastname) { console.log("hello, " + firstname + " " + lastname); } delay(outputperson, 3000, "john", "doe");
edit: patrick evans , point out in comments settimeout
already provides functionality being described here (as long you're not using ie <10). define delay
function this:
var delay = settimeout;
Comments
Post a Comment