Need Serious Javascript Help

windows

Guest
Simply put , I assembled some pieces of javascript that is suppose to randomly choose a link to a m3u file and activate it and reapet this once an hour, though the timing is set to every 15 seconds just for testing. below is the javascript I am using. One note the tiomer worked fine with a single file directly named, it when I added the random selector and array that I am having issues.<br /><br /><script language="JavaScript"><br /><!--<br /><br />function get_random(){<br /> var ranNum= Math.floor(Math.random()*4+1);<br /> return ranNum; <br /> }<br />function link(){<br /> var plist = get_random()<br /> link = new Array(3)<br /> link[1]="net radio/test 1.m3u";<br /> link[2]="net radio/test 2.m3u";<br /> link[3]="net radio/test 3.m3u";<br /> }<br /><br />var time = null<br />function move() {<br />window.location = (link[plist])<br />}<br />//--><br /></script><br /></head><br /><body><br /><br /><script language="JavaScript"><br /><!--<br /><br /> var y = window.setInterval('move()', 15000);<br /> <br />//--><br /></SCRIPT><br /><br />If there is any help out there I would really appreciate it.<br /><br />Thank you<br />Subdeacon William<!--content-->
I see a few problems with your code:<br /><br />1) Random number function outputs incorrect range of values:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->var ranNum= Math.floor(Math.random()*4+1);<!--c2--></div><!--ec2--><br />This code outputs a random integer in the list of 1, 2, 3, 4.<br /><br />To get a random integer in the list of 1, 2, 3, you should multiply Math.random by 3, not 4:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->var ranNum= Math.floor(Math.random()*3+1);<!--c2--></div><!--ec2--><br /><br />2) Code in link() function should not be in function at all.<br /><br />a) Using function name with same name as variable (array) can have unexpected results.<br /><br />b)<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->var plist = get_random()<!--c2--></div><!--ec2--><br />The above line of code should be in move() function so a new random integer is generated every time a new random link is needed (every 15 seconds in this case).<br /><br />c) <br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->link = new Array(3)<br />link[1]="net radio/test 1.m3u";<br />link[2]="net radio/test 2.m3u";<br />link[3]="net radio/test 3.m3u";<!--c2--></div><!--ec2--><br />This code should be outside of any function, so the link[] array will only be defined once and will be accessible to any javascript code on the page for as long as the page is loaded.<br /><br />Side notes: Arrays in javascript are zero-based. Defining an array with 'new Array(3)' will give you an array with 3 elements - [0], [1], and [2]. When you code assigns link[3], javascript will automatically expand the array to 4 elements to contain it, but I'd recommend defining the array with 4 elements.<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->var time = null<!--c2--></div><!--ec2--><br />Also, the above line of code does not appear to server any purpose in this code.<br /><br />With the changes I've described above, your code would look like this:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><script language="JavaScript"><br /><!--<br /><br />link = new Array(4)<br />link[1]="net radio/test 1.m3u";<br />link[2]="net radio/test 2.m3u";<br />link[3]="net radio/test 3.m3u";<br /><br />function get_random(){<br />var ranNum= Math.floor(Math.random()*3+1);<br />return ranNum;<br />}<br /><br />function move() {<br />var plist = get_random()<br />window.location = (link[plist])<br />}<br />//--><br /></script><br /></head><br /><body><br /><br /><script language="JavaScript"><br /><!--<br /><br />var y = window.setInterval('move()', 15000);<br /><br />//--><br /></script><!--c2--></div><!--ec2--><br />Hope this helps...<!--content-->
Thank you very much Dave, I had been stumbling through that for several days now. I have it up and running in my test box for now since I have to build the playlists that will be used on my actual site. I also included a little code for a start button since my sight will only change the playlist every hour or so. But here is the final code. when I get it all running on the site I will post a link so you can check it out. It hould fool you into think that it a actual streaming media when in reality it is not. Code:<br /><br /><br /><script language="JavaScript"><br /><!--<br /><br /> <br /> link = new Array(7)<br /> link[1]="net radio/test 1.m3u";<br /> link[2]="net radio/test 2.m3u";<br /> link[3]="net radio/test 3.m3u";<br /> link[4]="net radio/test 4.m3u";<br /> link[5]="net radio/test 5.m3u";<br /> link[6]="net radio/test 6.m3u";<br /><br /> function get_random(){<br /> var ranNum= Math.floor(Math.random()*6+1);<br /> return ranNum; <br /> }<br /> <br /><br />function move() {<br />var plist = get_random()<br />window.location = (link[plist])<br />}<br /><br /><br />//--><br /></script><br /></head><br /><body><br /><br /><script language="JavaScript"><br /><!--<br /><br /> var y = window.setInterval('move()', 15000);<br /> <br />//--><br /></SCRIPT><br /><center><br /><script language="JavaScript"><br /><!--<br /> <br /> var plist = get_random()<br /> document.write('<A HREF=http://www.totalchoicehosting.com/forums/lofiversion/index.php/"' +(link[plist])+ '"><IMG SRC="images/playup.gif"></A>') ;<br /> <br />//--><br /></SCRIPT><br /></center><br /><p align="center"><b><font size="4" color="#0000FF">Please keep this window open to receive the<br />stream.?</font></b></p><br /><br /><p align="center"><b><font size="4" color="#0000FF">You may minimize this window<br />and enjoy the site</font></b></p><br /><br /><br />Again thank you very much, <!--coloro:#FF0000--><span style="color:#FF0000"><!--/coloro--><b>you da man!!!!</b><!--colorc--></span><!--/colorc--><br />Subdeacon William<!--content-->
 
Top