以x ms间隔倒计时

时间:2017-05-23 08:02:21

标签: javascript

我希望我的循环每200毫秒倒数一次。

Exception in thread "main" java.lang.VerifyError: class org.elasticsearch.transport.Netty3Plugin overrides final method onModule.(Lorg/elasticsearch/common/network/NetworkModule;)V
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.elasticsearch.transport.client.PreBuiltTransportClient.<clinit>(PreBuiltTransportClient.java:84)
    at com.c1x.entity.Test.main(Test.java:22)

1 个答案:

答案 0 :(得分:2)

使用setInterval比任何for循环更有意义,因为setInterval被设计为每x毫秒执行一次代码:

&#13;
&#13;
let myArr = [3, 4, 5, 6];
var i = myArr.length - 1;

let interval = window.setInterval(() => {
  if (i >= 0) {
    console.log(myArr[i]);
    i--;
  } else {
    window.clearInterval(interval);
  }
}, 200);
&#13;
&#13;
&#13;