play audio only once in html page | Sololearn: Learn to code for FREE!


play audio only once in html page

This question maybe out of scope in sololearn but, i have received numerous helps here in sololearn so, i just want to ask here in hopes of getting some answers so please dont mind. So, what i am doing is I have a table 9x10 matrix numbered from 1 to 90. I am randomly picking a random each 5 seconds and gives that random number to the javascript. What i want to do is i want to update the cell color of the table that contains the random number and play and audio of the chosen number. I am able to change the cells color but my audio plays more then a thousand time in one second so it gives an effect like an echo so how can i solve this. ? The problem is that the javascript function that checks the random number in the table is called non stop so even if i set a variable 'count' to set it to 1 when the audio plays for the first time and put a condition to skip playing the audio if the condition doesn't apply, it doesnt work because the method or the function that check and plays the audio gets called everytime and the variable count is set back to its initial initialised number. Can i use cookies or session to store the number thats being randomly picked and use that to validate and play the audio only once ? Here is the link to the javascript and the table

6/9/2022 7:55:51 PM

Akang Toshi

5 Answers

New Answer


getData() should do just one thing, get data. There should be a separate function to check table every five seconds


Thanks EVERYONE i have solved it by using sessionStorage. Also, i have updated the changes in the code so you can check the comments there if you are curious :)


Umm.. Chris Coder i dont think it will work because the function getData() itself is being called non stop so even if we loop out by using a break or continue , right after it loops out from the function the same getData() function gets called again ..continuously. i am making a real time updates so it creates some issue . But , anyway thanks for your suggestion I'll try that


Yes delay is there random number is being picked every 5 seconds. 🙂


The problem is within that 5 sec duration the function getData () is called numerous times. Not only once .