Im working with notifications on my project which notify users when there are new orders. And update queue count every 5 seconds.
<audio id="foobar" src="{{URL::to('/')}}/assets/notif_sounds/plucky.mp3" preload="auto" autoplay="false">
<script>
setInterval(function(){
$.ajax({
type:'POST',
url:'{{URL::to('/')}}/get-count',
success:function(data)
{
$('#qCount').html(data);
var sample = document.getElementById("foobar");
sample.play();
}
});
}, 5000);
</script>
I tried this code snippet and still fire DOMException error
var promise = document.querySelector('audio').play();
if (promise !== undefined) {
promise.then(_ => {
// Autoplay started!
}).catch(error => {
// Autoplay was prevented.
// Show a "Play" button so that user can start playback.
});
}
Is there any way how to get rid of this error?