I would like to know if there is a way to allow a function to be called only after a few seconds. Example I have a function that emits sound when a new message comes up and it calls, but when I restart the page the function is called start logo, the notification should stay the sound should not be called, only when a message arrives yes yes must be called.
So, let's say you received a msg and it will not read now, but every time that msg is greater than 1 it calls the emitSom () function, but I want it to only call this function when ajax is working, but it returns with the notifications (so far so good), and calls the sound function, that is every time that page exchange the same calls the function that ends up irritating.Code:
Ajax function:
function notificarNMsg(id, tp_usuario, codigo, ultimos_id) {
ids = ultimos_id;
$.ajax({
url: 'pages/atualizacoes.php', /* URL que será chamada */
type: 'POST', /* Tipo da requisição */
data: {ANLid_usuario: id, ANLtp_usuario: tp_usuario, ANLcodigo: codigo, ultimos_id: ids},
dataType: 'json', /* Tipo de transmissão */
success: function (data) {
if (data.msg_nao_lidas >= 0) {
if (data.ids == 0) {
$("#areaMsg").html('<span></span>').removeClass("vibrarMsg");
} else if (data.ids !== 0 && data.msg_nao_lidas > 0) {
qtd_existente = $("#areaMsg span").text();
if (qtd_existente == '') {
qtd_existente = 0;
}
qtd_msg_total = parseInt(qtd_existente) + parseInt(data.msg_nao_lidas);
$("#areaMsg").html('<span>' + qtd_msg_total + '</span>').addClass("vibrarMsg");
acionarAlerta('./sons/nv_msg.wav');
trocarTituloPorTempo('- (' + qtd_msg_total + ') Mensagem');
if (ids != 0) {
ids = ids + ',' + data.ids.join(",");
} else {
ids = data.ids.join(",");
}
}
}
notificarNMsg(id, tp_usuario, codigo, ids);
}
});
}
Function that should be "Created" after a few seconds.
function acionarAlerta(caminho) {
var som = new Audio(caminho);
som.play();
}