javascript Call function 10 times with 1 second between

孤者浪人 提交于 2019-12-13 00:20:38

问题


How to call a function 10 times like

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

but with 1 sec between each call?


回答1:


You can use setInterval for repeated execution with intervals and then clearInterval after 10 invocations:

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

Added: But if you don't know how long it takes your callfunction to execute and the accurate timings between invocation starting points are not important it seems it's better to use setTimeout for reasons mentioned by Paul S and those described in this article.




回答2:


function callNTimes(func, num, delay) {
    if (!num) return;
    func();
    setTimeout(function() { callNTimes(func, num - 1, delay); }, delay);
}
callNTimes(callfunction, 10, 1000);

EDIT: The function basically says: make a call of the passed function, then after a bit, do it again 9 more times.




回答3:


Another solution

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



回答4:


You can try to use setInterval and use a variable to count up to 10. Try this:

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

Now use the setInterval:

setInterval(oneSecond, 1000);



回答5:


Similar to Amadan's answer but with a different style of closure which means you re-use instead of creating new functions

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

In this example, if you were to set times to either 0 or Infinity, it would run forever.




回答6:


I don't know if there's a proper name, but I use a repeater:

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

You can then use it like this:

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



回答7:


setInterval(function(){},1000);

Calls the function for every second...

You can also use setTimeout for your thing to work.



来源:https://stackoverflow.com/questions/21648264/javascript-call-function-10-times-with-1-second-between

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!