Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!