【问题标题】:javascript Call function 10 times with 1 second betweenjavascript调用函数10次,间隔1秒
【发布时间】:2014-02-08 16:03:31
【问题描述】:

如何将一个函数调用10次

for(x=0; x<10; x++) callfunction();

但每次通话之间有 1 秒?

【问题讨论】:

    标签: javascript timing function-call


    【解决方案1】:
    function callNTimes(func, num, delay) {
        if (!num) return;
        func();
        setTimeout(function() { callNTimes(func, num - 1, delay); }, delay);
    }
    callNTimes(callfunction, 10, 1000);
    

    编辑:该函数基本上是说:调用传递的函数,然后再执行 9 次。

    【讨论】:

    • 你能给我补充一些解释吗?
    • 我想我明白了,你在应用递归,很有趣。
    【解决方案2】:

    您可以使用 setInterval 以间隔重复执行,然后在 10 次调用后使用 clearInterval

    callfunction();
    var callCount = 1;
    var repeater = setInterval(function () {
      if (callCount < 10) {
        callfunction();
        callCount += 1;
      } else {
        clearInterval(repeater);
      }
    }, 1000);
    

    补充:但是如果你不知道你的 callfunction 需要多长时间执行并且调用起始点之间的准确时间并不重要,那么由于 Paul 提到的原因,似乎最好使用 setTimeout S 和this article 中描述的那些。

    【讨论】:

    • 我建议不要使用setInterval,尤其是当您不知道callfunction 需要多长时间才能完成调用时(如果代码在不到 1 秒的时间内重复使用则更重要间隔),因为您最终可能会遇到讨厌的..级联?
    • @PaulS。嗯,你是对的。我找到了相关文章,现在同意在一般情况下 setTimeout 似乎更合适。不过,在某些情况下,调用起点之间的准确计时更为重要(以及何时需要 (0,1] 秒才能完成)。
    【解决方案3】:

    另一种解决方案

    for(var x=0; x<10; x++) window.setTimeout(callfunction, 1000 * x);
    

    【讨论】:

      【解决方案4】:

      您可以尝试使用setInterval 并使用变量数到10。试试这个:

      var number = 1;
      function oneSecond () {
        if(number <= 10) {
          // execute code here..
          number++;
        }
      };
      

      现在使用 setInterval:

      setInterval(oneSecond, 1000);
      

      【讨论】:

      • @Rudie,是的!你说得对,我只是把变量和函数弄糊涂了对不起,谢谢:)
      • 这会执行10次以上。
      • var number == '1'; 不会创建变量。 == 用于比较。
      • @Rudie,你又是对的。我想我现在需要休息一下-_-
      【解决方案5】:

      类似于Amadan's answer,但闭包风格不同,这意味着您可以重复使用而不是创建新函数

      function call(fn, /* ms */ every, /* int */ times) {
          var repeater = function () {
              fn();
              if (--times) window.setTimeout(repeater, every);
          };
          repeater(); // start loop
      }
      // use it
      var i = 0;
      call(function () {console.log(++i);}, 1e3, 10); // 1e3 is 1 second
      // 1 to 10 gets logged over 10 seconds
      

      在本例中,如果您将times 设置为0Infinity,它将永远运行。

      【讨论】:

        【解决方案6】:

        我不知道有没有合适的名字,但我用的是中继器:

        function Repeater(callback, delay, count) {
            var self = this;
            this.timer = setTimeout(function() {self.run();},delay);
            this.callback = callback;
            this.delay = delay;
            this.timesLeft = count;
            this.lastCalled = new Date().getTime();
        }
        Repeater.prototype.run = function() {
            var self = this;
            this.timesLeft--;
            this.callback();
            this.lastCalled = new Date().getTime();
            if( this.timesLeft > 0) {
                this.timer = setTimeout(function() {self.run();},this.delay);
            }
        }
        Repeater.prototype.changeDelay = function(newdelay) {
            var self = this;
            clearTimeout(this.timer);
            this.timer = setTimeout(function() {self.run();},
                                  newdelay-new Date().getTime()+lastcalled);
            this.delay = newdelay;
        }
        Repeater.prototype.changeCount = function(newcount) {
            var self = this;
            if( this.timesLeft == 0) {
                this.timer = setTimeout(function() {self.run();},this.delay);
            }
            this.timesLeft = newcount;
            if( this.timesLeft == 0) clearTimeout(this.timer);
        }
        

        然后你可以像这样使用它:

        new Repeater(callfunction, 1000, 10); // 1 second delay, 10 times
        

        【讨论】:

        • 这实际上不能正常工作,因为当setTimeout() 调用run() 时,this 值将不会指向Repeater 对象。您必须使用闭包或.bind() 来修复。
        • @jfriend00 Eeeehhhhh...如果您需要参考中继器本身,我只有var rpt = new Repeater(function() { doSomethingWith(rpt); }, delay, conut);
        • 也许您没有理解我的评论?当您将 this.run 传递给 setTimeout() 时,setTimeout() 保存的所有内容都是对 run() 的函数引用,并且没有使用正确的 this 值调用它,因此您的代码将无法工作。
        • 啊,我明白你的意思了。我的错。
        • 你可以在你的函数存根中使用self.run()而不是self.run.call(this);
        【解决方案7】:

        const functionCounterTimer = (callCount) => {
          if (callCount < 10) {
            setTimeout(() => {
              ++callCount
              console.log("Function Call ", callCount);
              functionCounterTimer(callCount);
            }, 1000);
          }
        }
        
        functionCounterTimer(0);

        以上是我对类似问题的处理方法。

        【讨论】:

          【解决方案8】:
          setInterval(function(){},1000);
          

          每秒调用一次函数...

          你也可以使用 setTimeout 来让你的东西工作。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-07-05
            • 2018-02-16
            • 2023-03-16
            • 1970-01-01
            • 1970-01-01
            • 2018-10-12
            • 2014-04-23
            • 1970-01-01
            相关资源
            最近更新 更多