sub-deacon Posted April 11, 2006 Posted April 11, 2006 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. <script language="JavaScript"> <!-- function get_random(){ var ranNum= Math.floor(Math.random()*4+1); return ranNum; } function link(){ var plist = get_random() link = new Array(3) link[1]="net radio/test 1.m3u"; link[2]="net radio/test 2.m3u"; link[3]="net radio/test 3.m3u"; } var time = null function move() { window.location = (link[plist]) } //--> </script> </head> <body> <script language="JavaScript"> <!-- var y = window.setInterval('move()', 15000); //--> </SCRIPT> If there is any help out there I would really appreciate it. Thank you Subdeacon William Quote
TweezerMan Posted April 12, 2006 Posted April 12, 2006 I see a few problems with your code: 1) Random number function outputs incorrect range of values: >var ranNum= Math.floor(Math.random()*4+1); This code outputs a random integer in the list of 1, 2, 3, 4. To get a random integer in the list of 1, 2, 3, you should multiply Math.random by 3, not 4: >var ranNum= Math.floor(Math.random()*3+1); 2) Code in link() function should not be in function at all. a) Using function name with same name as variable (array) can have unexpected results. >var plist = get_random() 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). c) >link = new Array(3) link[1]="net radio/test 1.m3u"; link[2]="net radio/test 2.m3u"; link[3]="net radio/test 3.m3u"; 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. 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. >var time = null Also, the above line of code does not appear to server any purpose in this code. With the changes I've described above, your code would look like this: ><script language="JavaScript"> <!-- link = new Array(4) link[1]="net radio/test 1.m3u"; link[2]="net radio/test 2.m3u"; link[3]="net radio/test 3.m3u"; function get_random(){ var ranNum= Math.floor(Math.random()*3+1); return ranNum; } function move() { var plist = get_random() window.location = (link[plist]) } //--> </script> </head> <body> <script language="JavaScript"> <!-- var y = window.setInterval('move()', 15000); //--> </script> Hope this helps... Quote
sub-deacon Posted April 12, 2006 Author Posted April 12, 2006 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: <script language="JavaScript"> <!-- link = new Array(7) link[1]="net radio/test 1.m3u"; link[2]="net radio/test 2.m3u"; link[3]="net radio/test 3.m3u"; link[4]="net radio/test 4.m3u"; link[5]="net radio/test 5.m3u"; link[6]="net radio/test 6.m3u"; function get_random(){ var ranNum= Math.floor(Math.random()*6+1); return ranNum; } function move() { var plist = get_random() window.location = (link[plist]) } //--> </script> </head> <body> <script language="JavaScript"> <!-- var y = window.setInterval('move()', 15000); //--> </SCRIPT> <center> <script language="JavaScript"> <!-- var plist = get_random() document.write('<A HREF="' +(link[plist])+ '"><IMG SRC="images/playup.gif"></A>') ; //--> </SCRIPT> </center> <p align="center"><b><font size="4" color="#0000FF">Please keep this window open to receive the stream. </font></b></p> <p align="center"><b><font size="4" color="#0000FF">You may minimize this window and enjoy the site</font></b></p> Again thank you very much, you da man!!!! Subdeacon William Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.