Allow a function to be called only after a time the page was loaded

1

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();
}
    
asked by anonymous 09.10.2015 / 06:04

2 answers

1

Wait for the DOMContentLoaded event of the document - this will indicate that the page has finished. After that, you can implement a TimeOut to run after N milliseconds.

// Aguarde a finalização da carga da página:
document.addEventListener("DOMContentLoaded", function(event) { 
    // Dispare um evento daqui a 5 segundos (5000 milissegundos):
    window.setTimeout(function () {
        // Redirecionando...
        location.href = "https://www.google.com.br";
    }, 5000);
});
    
09.10.2015 / 16:02
0

You can listen for the load event of window , after waiting for a while (using the setTimeout function) and then calling its function.

window.addEventListener("load", function load(event) {
    setTimeout(function () {
        acionarAlerta("seu_caminho");
    }, 5000);
});

or

window.onload = function (event) {
    setTimeout(function () {
        acionarAlerta("seu_caminho");
    }, 5000);
});
    
03.02.2017 / 04:55