How to make my function so it can run automatically even when the webpage is not currently active (run the function in background basically)

have a list, in which the names of the songs I want to play are placed in an order…when i have to play the next one, my function gets the currently playing song’s index, and plays the next one in the list..

toPlay = autoPlayOrder[(autoPlayOrder.indexOf(playing)) + 1]
play(toPlay)

here,

  1. autoPlayOrder is the list which has the songs to play
  2. playing is the song which is currently playing
  3. play() is a function I have that just plays the song

Now the system works as expected when the page is loaded but when im on another webpage, then when the song stops the next one isnt autoplayed…im guessing this is due to the reason that javascript isnt allowed to run when the page is not active

Just another side note, I was almost facing the same issue when i tried to make a loop song feature, but managed to work around that by using the inbuilt loop feature in an audio element in html..

Thanks!