I have a JavaScript that runs an audio, it is worth mentioning that I use ASP.NET and use SignalR, the question here is that when I load the screen, sometimes the sound enters a cycle and plays to infinity I leave the code below
$(function () {
// Declare a proxy to reference the hub.
var notifications = $.connection.MyHub;
//debugger;
// Create a function that the hub can call to broadcast messages.
notifications.client.updateMessages = function () {
getAllMessages()
};
// Start the connection.
$.connection.hub.start().done(function () {
//alert("connection started")
//notifications.onconn();
getAllMessages();
}).fail(function (e) {
alert(e);
});
});
//var colorBk = "", llamadaBk;
function getAllMessages()
{
var tbl = $('#messagesTable');
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();
var audio = document.getElementById('audio');
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
$.ajax({
url: '/Home/GetMessages',
contentType: 'application/html ; charset:utf-8',
type: 'GET',
dataType: 'html'
}).success(function (result) {
var a2 = JSON.parse(result);
tbl.empty();
$.each(a2, function (key, value) {
//alert(colorBk);
//$("#date").html("<b>" + dd + '/' + mm + '/' + yyyy + "</b>");
//document.getElementById("date").innerHTML = dd + '/' + mm + '/' + yyyy;
$("#descripcionANDON").html("<b>" + value.status + "</b>");
document.bgColor = value.color;
//$("#tab").html("<audio id='audio' controls><source type='audio/wav' src=''></audio>");
tbl.append('<tr>' + '<td>' + value.maquina + '</td>' + '</tr>');
if (value.color == 'Red') {
audio.setAttribute('src', '/sounds/InicioParo.wav');
} else if (value.color == 'Yellow') {
audio.setAttribute('src', '/sounds/GM140A MMTO.mp3');
} else {
audio.setAttribute('src', '/sounds/FinParo.wav');
if (value.status == '') {
$("#descripcionANDON").html("<b> Operación Iniciada </b>");
document.bgColor = "Green"
}
}
audio = document.getElementById('audio');
audio.play();
});
//alert(audio.paused);
//if (audio.duration > 0 && !audio.paused) {
//} else {
//}
})
}