Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help on how to create sequential timed functions

Status
Not open for further replies.

DuckyMoon

Technical User
Jul 8, 2000
2
US
Hey everyone,<br>I'm trying to get an answer to this question, since I am on a &quot;can we have that yesterday&quot; timetable to get this site up.&nbsp;&nbsp;Hope someone can help me!!<br><br>I'm trying to write a Javascript function that will play a succession of notes using Beatnik. The pitch of the notes is computed from variable (pc) passed to the function as arguments. My problem is timing the notes.&nbsp;&nbsp;In my current version (see below) the notes play simultaneously. I tried nesting a function call for the playNote statement as the first argument to the setTimeout functions, but then notes still play all together.<br><br>So what I want to do is play each note in succession with a specific delay in between notes. Any suggestions?<br><br>function playline (pc1, pc2, pc3) {<br>pitch1 = pc1 + 60<br>pitch2 = pc2 + 60<br>pitch3 = pc3 + 60<br><br>notePlayer.playNote (1,0,24,pitch1,127,1000)<br>&nbsp;&nbsp;setTimeout(&quot;&quot;,1000)<br>notePlayer.playNote (1,0,24,pitch2,127,1000)<br>&nbsp;&nbsp;setTimeout(&quot;&quot;,2000)<br>notePlayer.playNote (1,0,24,pitch3,127,1000)<br>;<br>}<br>I hope that makes sense!&nbsp;&nbsp;Thanks :)<br>
 
try this instead:<br><br>notePlayer.playNote(1,0,24,pitch1,127,1000);<br>setTimeout(&quot;notePlayer.playNote(1,0,24,pitch2,127,1000)&quot;,1000);<br>setTimeout(&quot;notePlayer.playNote(1,0,24,pitch3,127,1000)&quot;,2000);<br><br>when you say setTimeout(...), javascript waits the specifyed number of miliseconds, and then executes the code in the quotes. untill the time is up, javascript continues normally.<br><br>the way you are using the setTimeout is incorrect.<br><br>in the example above:<br>1) the first note command is read, and is played instantly<br>2) javascript sees the setTimeout command for the second note, but with a one-second delay. It loads that command into a timer, which executes it when the time is up<br>3) javascript sees the setTimeout command for the third note, also with a delay. this time, the delay is two seconds. Javascript stores this command into yet another timer, which will execute this command when the two seconds is up.<br><br>one second later:<br>the command in the first timer fires, playing the second note. while this command is executing, all other scripting is stopped, as javascript can only process one command at a time.<br><br>another second later (two total seconds):<br>the command in the second timer fires, playing the third note. the same goes for this command, as for the one-second delay.<br><br>I hope I didn't confuse you. the code at the top should work. <p>theEclipse<br><a href=mailto:eclipse_web@hotmail.com>eclipse_web@hotmail.com</a><br><a href=robacarp.webjump.com>robacarp.webjump.com</a><br>**-Trying to build a documentation of a Javascript DOM, crossbrowser, of course. E-mail me if you know of any little known events and/or methods, etc.
 
Thank you SO much for taking the time to help me with this.&nbsp;&nbsp;I am going to go try it now.&nbsp;&nbsp;I really really appreciate your consideration and assistance.<br><br><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top