Parallel processes in js

advertisements

I have one question about parallel processes in javascript. I mean async processes. I need play two soundtracks at the same time(sync). But, if I call it in a row, tracks can play a little bit async.:

audio.play();
audio2.play();

I think and I almost know, that javascript has opp. for processing async. actions. How?

Thanks!


Easiest way would be to use the fantastic library called async

Then you could do

async.parallel([
    function(callback) {
        audio.play();
        callback();
    },
    function(callback) {
        audio2.play();
        callback();
    }], function() {
          console.log("Both sounds running together");
});

Or I believe you can just wrap each .play like this

setTimeout(function() {
    audio.play();
});
setTimeout(function() {
    audio2.play();
});