I am trying to write a program to assist me in learning to play the piano. I have set up three buttons that will each call a function to randomly spit out chord names.
The problem I am having is that I want a 3 second delay within the while loop between chord names. I have looked around on the net and can't seem to see how to do it. Here is the code for the first function:
<html>
<head>
<script type="text/javascript">
MAJORS = new Array('A','B','C','D','E','F','G');
MINORS = new Array('Am','Bm','Cm','Dm','Em','Fm','Gm');
MIXED = new Array('A','B','C','D','E','F','G','Am','Bm','Cm','Dm','Em','Fm','Gm');
var iterations = 20;
var timeDelay = 3000;
function randomiseMajors(){
var i =0;
var rand;
while(i<iterations){
rand = parseInt(Math.floor(Math.random()*6));
//alert(rand);
document.writeln(MAJORS[rand]);
i++;
setTimeout(delay(),timeDelay);
}
}
Also, is it easy to display the chord names one at a time? ie when a new chord is displayed the previous one disappears. (This question is not so important I am more interested in the Timeout question).
Try setTimeout(delay,timeDelay); without () and do not use document.write (or writeln) which is made for an open document and is not valid in a function called after the document is close. Use for example a div with a innerHTML
Code:
// replace this
document.writeln(MAJORS[rand]);
// by
document getElementById('rsp').innerHTML+=MAJORS[rand];
// with a
<div id="rsp"></div>
Thanks for that advice Julien, it displays better now and I can run it more than once. The time delay between iterations still doesn't work but that doesn't matter because I am going to dissect Bionoid's code and see how he did the time delay and try and replicate that.
Thanks Bionoid, program works well. I am going to try and learn a few things from your code.
Bookmarks