javascript调用函数10次,间隔1秒 [英] javascript Call function 10 times with 1 second between

查看:42
本文介绍了javascript调用函数10次,间隔1秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何调用一个函数10次

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

但每次通话之间间隔 1 秒?

解决方案

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

callfunction();var callCount = 1;var repeater = setInterval(function () {如果(调用计数<10){调用函数();呼叫计数 += 1;} 别的 {清除间隔(中继器);}}, 1000);

补充:但是如果您不知道执行调用函数需要多长时间并且调用起点之间的准确计时并不重要,那么出于 Paul 提到的原因,似乎最好使用 setTimeoutS 和 本文 中描述的那些.>

How to call a function 10 times like

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

but with 1 sec between each call?

解决方案

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.

这篇关于javascript调用函数10次,间隔1秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆