0

This is my first Java Script app. I am writing a music application, and one of its parts must play the notes that are stored in a two-dimensional array in turn. I use setTimeout() for this with a periodically increasing delay so that the notes are played sequentially. the problem is that setTimeout() causes a function play(...) without delay and all notes sound simultaneously UPD: I changed my code as advised in the comments, replaced the fore..in with foreach, the problem remained the same

 function playAll() {
        var j = 0;
        noteArr.forEach(function (item,i,arr) {
            var delay = 500 * j;
            j++;
            item.forEach(function(item,i,arr){
                setTimeout(play(item), delay);
            });
        });
    }
nehippi
  • 21
  • 4
  • setTimeout(() => { play(noteArr[key][key2]); }, delay); in ES6 way or setTimeout(function(){ play(noteArr[key][key2]); }, delay) will do the hack. – Hyunnique May 05 '20 at 23:52
  • @Hyunnique that won't work. By the time those run, `key` and `key2` will be at their final values. OP would do better using `forEach` instead of `for..in` – Phil May 06 '20 at 00:09
  • @Phil, can you open my question? Solution by link does not suit me – nehippi May 06 '20 at 00:35
  • I guarantee the answer to your question can be found in the linked duplicate – Phil May 06 '20 at 00:36
  • @Phil I'm sorry, it really helped – nehippi May 06 '20 at 00:48

0 Answers0