Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Very strange bug! (Sugarcane, Javascript)

yunyun
edited April 2016 in Help! with 1.x
I'm trying to write a function which simulates dialing:
<<set $nmbrs=["dial0.wav","dial1.wav","dial2.wav","dial3.wav","dial4.wav","dial5.wav","dial6.wav","dial7.wav","dial8.wav","dial9.wav"]>>

<<set $dial=
function(number)
{function delay(i)
 {var d=eval(number[i]);if (!d) d=10;return 666+d*160;}
 var code='<<timedcontinue 0.5>>';dur=500;
 for (var i=0;i<number.length;++i)
 {t=$nmbrs[eval(number[i])];
   code=code+'<<playsound "'+t+'">><<timedcontinue '+delay(i)/1000+'>>\n';
  dur+=delay(i);
 }
//alert(code);
new Wikifier(document.createElement('div'), code);
 return dur/1000;
}
>>

<<timedcontinue $dial("99876543201")>>\
(My modification of timedcontinue macro accepts the time in seconds only, the rest is the same as in the standard one)
If I uncomment the alert, everything works great, and this is what the alert shows:
<<timedcontinue 0.5>><<playsound "dial9.wav">><<timedcontinue 2.106>>
<<playsound "dial9.wav">><<timedcontinue 2.106>>
<<playsound "dial8.wav">><<timedcontinue 1.946>>
<<playsound "dial7.wav">><<timedcontinue 1.786>>
<<playsound "dial6.wav">><<timedcontinue 1.626>>
<<playsound "dial5.wav">><<timedcontinue 1.466>>
<<playsound "dial4.wav">><<timedcontinue 1.306>>
<<playsound "dial3.wav">><<timedcontinue 1.146>>
<<playsound "dial2.wav">><<timedcontinue 0.986>>
<<playsound "dial0.wav">><<timedcontinue 2.266>>
<<playsound "dial1.wav">><<timedcontinue 0.826>>
These delays are correct for every digit and works OK.
But if I remove the alert, the first delay becomes about 10 times shorter it should be! Not 0, however (I found it out by increasing all delays 10 times - then the 1st one becomes normal, while all the rest becomes huge). So the second digit sound starts playing way before the first one ends. That's true for any first digit - 0,9,1, etc.
What's the reason and how to fix it?

Comments

  • I tried another variant, using setTimeOut directly instead of timedcontinue (and my function $playsound which calls the playsound macro handler), but the result is the same :(
    <<set $dial=
    function(number)
    {function delay(i)
     {var d=eval(number[i]);if (!d) d=10;return 666+d*160;}
     var code='';dur=500;
     for (var i=0;i<number.length;++i)
     {t=$nmbrs[eval(number[i])];
       code=code+'<<set setTimeout(function(){$playsound("'+t+'")},'+dur+')>>\n';
      dur+=delay(i);
     }
    //alert(code);
    new Wikifier(document.createElement('div'), code);
     return dur/1000;
    }
    >>
    
    Only in this case even uncommenting alert does not help.

    I'm completely stuck. Pleeease help!
Sign In or Register to comment.