简体   繁体   中英

Implementing a delay function using setTimeout() with passing optional arguments (Javascript)

I am trying to get to grips with Javascript by implementing intermediate functions from scratch. Currently trying to implement a delay function that executes an arbitrary function passed as an argument after a waiting time (wait). This also needs to be able to forward additionally passed arguments as extra arguments for the function being delayed.

What I have made so far isn't calling the function within the setTimeout(). Im not sure if its a syntax error or ive just missed the point completely. I have looked through similar questions on here and tried to implement some of the suggested results, however none seem to take the additional arguments aspect into consideration. Anyway, here is what I currently have.

    var exampleDelay = function (func, wait) {
      return function () {
        setTimeout(func.apply(this, arguments), wait);
      }
    };

Any help tackling this would be appreciated (or if anyone can point me to an answer I may have missed).

Fran beat me to it but just for variety. if you want to supply all the params at once this might be an option

  var exampleDelay = function(callback,wait,args) { var args = [].slice.call(arguments) // get the parent arguments and convert to an array args.splice(0,2); // remove the first two argument which are the fuction supplied and the wait time // a fuction to call the supplied function var callnow = function() { var params = arguments; // get the child arguments var context = this; setTimeout(function(){ callback.apply(context, params) // call the function }, wait); } callnow.apply( this, args ) // use apply to supply the arguments extracted from the parrent }; exampleDelay(console.log, 1000,"hey") exampleDelay(console.log, 5,"hey", "there") 

 callnow.apply( this, args ) // we then call the function with apply and supply args extracted from the parent 

well, you can handle function validation later to make sure that the first argument is a function

The function you return from exampleDelay is the one you call later one. To preserve the arguments at the time of calling that function until the time the timer executes it you can wrap the intended function in an anonymous function within the timer. Then inside you can use apply to pass the previously stored arguments. Similar to the below.

 var exampleDelay = function(func, wait) { return function() { var params = arguments; var context = this; setTimeout(function(){ func.apply(context, params) }, wait); } }; var func1 = function(a, b) { console.log(a + b); } var func2 = function(a, b, c) { console.log(a, b, c); } var x = exampleDelay(func1, 1000); var y = exampleDelay(func2, 2000); x(12, 15); y('How', 'Are', 'You?'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM