简体   繁体   English

使用setTimeout()实现延迟函数并传递可选参数(Javascript)

[英]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. 我试图通过从头开始实现中间函数来掌握Javascript。 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(). 到目前为止我所做的并不是在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. Fran打败了我,但只是为了变化。 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. 您从exampleDelay返回的函数是您稍后调用的函数。 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. 要在调用该函数时保留arguments ,直到计时器执行它,您可以将预期函数包装在计时器内的匿名函数中。 Then inside you can use apply to pass the previously stored arguments. 然后在里面你可以使用apply来传递先前存储的参数。 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?'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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