Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
Javascript playing an endless loop audio file without delay

Title is pretty much self explanatory.

I want to create an endless loop with JavaScript but without stopping between reading (again) the same exact file.

Here is my code:

function playMySong(url)
{
    var audio=document.createElement('audio');
    audio.style.display="none";
    audio.src=url;
    audio.autoplay=true;
    audio.onended=function()
    {
        playMySong('whiteNoise.wav');
    };
    document.body.appendChild(audio);
}

playMySong('whiteNoise.wav');

My code works (almost) perfectly but I want to get read of the tiny delay between the end of the sound to the beginning of the same exact file.

not it's:

[whiteNoise.wav][0.2ms delay][whiteNoise.wav]

I want:

[whiteNoise.wav][whiteNoise.wav]

:)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are loading the audio file again and again when it reaches the end. Obviously, loading resources will take time.

Why don't you use a simple loop attribute to the audio element? It will loop the audio clip itself without loading the resource again and again.

function playMySong(url)
{
    var audio=document.createElement('audio');
    audio.style.display="none";
    audio.src=url;
    audio.autoplay=true;
    audio.loop = true;
    document.body.appendChild(audio);
}

playMySong('whiteNoise.wav');

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!