【问题标题】:JavaScript setInterval function may or may not have parametersJavaScript setInterval 函数可能有也可能没有参数
【发布时间】:2016-05-28 17:27:19
【问题描述】:

我正在研究一个简单的高阶函数delay,它将调用函数参数func,然后延迟以wait 传递的时间量。我已经阅读了关于在哪里输入参数的其他答案,但我找到的答案都没有解决我需要学习的内容:我应该在哪里以及如何允许可能会或可能不会传递给 func 的参数?

原始说明:“在等待毫秒后调用 func。任何附加参数都会在调用时提供给 func。”

这是基本的开始:

function delay(func, wait) {
    setInterval(func, wait);
}

关于 SO 的另一个答案指出,可以使用匿名函数来包装 func 参数,以便可以在那里传递参数,但我还没有成功构建它。

非常感谢您的指导。

【问题讨论】:

  • 你试过window.setTimeout吗?

标签: javascript function


【解决方案1】:

听起来你需要使用arguments伪数组和Function#apply

function delay(func, wait) {
  // get all arguments after the first two
  var args = Array.prototype.slice.call(arguments, 2);

  setTimeout(function() {
    func.apply(null, args);
  }, wait);
}

示例:

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");

编辑: 正如 Patrick Evans 和我在 cmets setTimeout 中指出的那样,已经 提供了这里描述的功能(只要您不使用 IE delay 函数:

var delay = setTimeout;

【讨论】:

  • 由于延迟量之后的所有参数都作为参数传递给传递的函数,你可以只做setTimeout.apply(null,[].slice.call(arguments)),或者只是delay = setTimeout都等同于相同的行为......
【解决方案2】:

我认为建模它的正确方法是承认延迟只是副作用,不应该有参数。延迟应该只是延迟而已。

您可能还想使用新的(ish)标准语言方式来执行信号异步操作的 Promise:

const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

请参阅this question and answer,了解new Promise 部分的作用以及promise API 的外观。

然后,您可以将延迟与函数调用分开使用:

delay(1000).then(fn);
delay(1000).then(() => fn(arg1, arg2, arg3));

等等。

如果你想继续使用回调

setTimeout 实际上已经完成了您的要求,setTimeout(fn, 1000) 是调用超时的简单方法,但是您实际上可以在延迟量之后将其他参数传递给函数,并且将使用它们调用函数。

【讨论】:

    【解决方案3】:

    可能是链接这个:

    function delay(func, wait,funcArguments) {
        setInterval(function() {
            funcArguments = funcArguments  || [];
            func.call(null, funcArguments);
        }, wait);
    }
    

    funcArguments 是带参数的数组。

    【讨论】:

      【解决方案4】:

      通常在库中,他们将其命名为 debounce,您可以像这样简单地实现它:

      function debounce(func, wait=0,args=[]) {
          setTimeout(function() {
              func.call({}, args);
          }, wait);
      }
      

      但是正确的实现,就像 lodash 的 here 一样,要复杂得多。

      【讨论】:

        【解决方案5】:

        你可以只添加setInterval的参数

        var intervalID = window.setInterval(func, delay[, param1, param2, ...]);
        

        function delay(func, wait, param) {
            setInterval(func, wait, param);
        }
        
        
        function hello(x) {
            var p = document.createElement('p');
            p.innerHTML = 'Hello ' + x + '!';
            document.body.appendChild(p);
        }
        
        delay(hello, 1000, 'world');

        【讨论】:

          【解决方案6】:

          这是一个简单的解决方案:

          function delay(func, wait, ...param) {
            setTimeout(function(){func(param);}, wait);
          }
          function doit(a){console.log(a);}
          
          delay(doit,500,'test1','test2');
          //['test1','test2']
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多