0

I have done something wrong the following code is not setting the delay between executing the beeps. It is looping 3 times as expected and beeping 3 times, but all at the same time.

It seems my interval value of 3000 is not being activated.

I am probably doing it the wrong way. What have I misunderstood ?

var myBeep = new Audio('pins/beep1Sec.mp3');

var dbg = "";

function doBeeps(Start, NoOfTimes, Interval) {
    console.log(Start);
    bp = document.getElementById("beep");  // just for testing

    myBeep.play();
    console.log("Playing Beep");
    dbg += " beep : " + Start;
    bp.innerHTML = dbg;
    Start++;

    if( Start <= NoOfTimes ){
        console.log ("Start=" + Start + " ~~~ NoOfTimes=" + NoOfTimes + " ~~~ Interval=" + Interval);
        setTimeout( doBeeps(Start,NoOfTimes,Interval),Interval);
    }
}

doBeeps(1,3, 3000);

MY ERROR

Thanks for pointing me to a satisfactory answer. My error was not making my call to itself a function in SetTimeout. This is what it should look like

setTimeout( function () {doBeeps(Start,NoOfTimes,Interval)} ,Interval);

One day the mists of javascript internals might clear !!

mcl
  • 109
  • 12

0 Answers0