'Random sound Player
I'm trying to make it so that every 3 minutes, a new song gets played... but its not working and i'm not sure why... heres my code,
window.onload = function(){
song();
setInterval(song, 180000);
}
var song1 = new Audio("music/song1.mp3");
var song2 = new Audio("music/song2.mp3");
var song3 = new Audio("music/song3.mp3");
var song4 = new Audio("music/song4.mp3");
var song5 = new Audio("music/song5.mp3");
var song6 = new Audio("music/song6.mp3");
var song7 = new Audio("music/song7.mp3");
var song8 = new Audio("music/song8.mp3");
var songNum = 1;
function song(){
songNum = Math.floor(Math.random() * 8) + 1;
if(songNum == 1){
song1.load();
song1.play();
} else if(songNum == 2){
song2.load();
song2.play();
} else if(songNum == 3){
song3.load();
song3.play();
} else if(songNum == 4){
song4.load();
song4.play();
} else if(songNum == 5){
song5.load();
song5.play();
} else if(songNum == 6){
song6.load();
song6.play();
} else if(songNum == 7){
song7.load();
song7.play();
} else if(songNum == 8){
song8.load();
song8.play();
}
}
I've tried to change many things but I can't figure out whats causing the issue, it seems to work sometimes but then I leave and go back into the page and it doesn't work. If you know the issue please help.
Solution 1:[1]
I found the issue, for anyone who has a similar issue I will leave this comment up with the answer... The issue was that it was calling the function before the user interacted with the site, so all I did was make it call after the 'click to continue' page that i had setup as a tutorial
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RMCMinor |
