Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
Expose function reference to be accessed from outside

I have this code, where I add my button click listeners to call my startTempMonit function

$(function () {
    const inter;
    $("#buttonStartTemp").click(function () {
        inter = startTempMonit(onFetchCallback.store);
    });

    $("#buttonStopTemp").click(function () {
        clearInterval(inter); //inter is undefined
    });
});

function startTempMonit(callback) {
    var time = 0;
    const Interval= setInterval(function () {
        ...
    }, 500);
    return Interval;
}

I would like o to know how to expose the returned const Interval in the startTempMonit so as to be able to clear it when I click the other button (#buttonStopTemp).

I also tried defining const inter; in the outest scope, which I would not like, but its does not work either.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have to initialize a constant. Instead, declare inter with var:

$(function () {
    var inter;
    $("#buttonStartTemp").click(function () {
        inter = startTempMonit(()=>console.log(1));
    });

    $("#buttonStopTemp").click(function () {
        clearInterval(inter);
    });
});

function startTempMonit(callback) {
    var time = 0;
    const Interval= setInterval(callback, 500);
    return Interval;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="buttonStartTemp">Start</button>
<button id="buttonStopTemp">Stop</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda