title: Is there better way to loop item of array using forEach and make repeat it using setTimout/Interval
body: <p>Is there a better way to loop item of an array using forEach and make repeat it using setTimout/Interval or somethin' else?</p>
  <p>I need that each item of the array was repeated each value of delay for some time (duration)</p>
  <p>Here is link
  <a href="https://codesandbox.io/s/to-loop-an-element-of-array-dm1eq?file=/src/index.js:140-154" rel="nofollow noreferrer">https://codesandbox.io/s/to-loop-an-element-of-array-dm1eq?file=/src/index.js:140-154</a></p>
  <p>Here is the code but I think it's a difficult solution</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const arr = [
    { delay: 1_000, duration: 3_000, msg: "Hello World" }, //repeat msg each sec for 3 sec
    { delay: 2_000, duration: 4_000, msg: "Sup?" } //repeat msg each 2 sec for 4 sec
  ];
  
  class LoopElem {
    constructor(configs) {
      this.count = 0;
      this.sumDelay = 0;
      this.toLoopElem(configs);
    }
  
    toLoopElem = (configs) =&gt; {
      const that = this;
      const { delay, duration, msg } = configs[that.count];
      setTimeout(function request() {
        that.sumDelay += delay;
        console.log(msg); // each delay shows msg
        if (that.sumDelay &gt;= duration) {
          that.sumDelay = 0;
          that.count += 1;
          // if array have already no item stop repeat it
          if (!configs[that.count]) {
            return null;
          }
          setTimeout(that.toLoopElem, configs[that.count].delay, configs);
          return null;
        }
        setTimeout(request, delay);
      }, delay);
    };
  }
  
  new LoopElem(arr);</code></pre>
  </div>
  </div>
  </p>