So, I'm working on a music program that requires multiple javascript elements to sync to another. I use setInterval, which works very well initially, but over time, the elements gradually stop synchronizing, which is bad with a music program.
I read on the internet that setTimeout is more accurate, and you can somehow set setTimeout loops, however I did not find a generic version that illustrates how this is possible. Can someone just show me a basic example of using setTimeout for something endlessly.
Thank. Alternatively, if there is a way to achieve more synchronous results using setInterval or even another function, please let me know.
EDIT:
Basically, I have some functions like:
//drums setInterval(function { //code for the drums playing goes here },8000); //chords setInterval(function { //code for the chords playing goes here },1000); //bass setInterval(function { //code for the bass playing goes here },500);
At first it works very well, but within about a minute the sounds become noticeably out of sync, as I read with setInterval, I read that setTimeout can be more consistently accurate.
javascript settimeout timing setinterval
user3084366 03 Mar. '14 at 18:11 2014-03-03 18:11
source share