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

115
Views
My Bootstrap Modal button doesn't stop a setInterval unless it is clicked in the first seconds. How to fix this

I have a function that reloads the page every 2 seconds and it starts with the first load. I want this reload to stop as soon as I click the Modal Bootstrap 4.7 button. My code works ok if I click within the first 2 seconds interval. Otherwise it no longer obeys my call. why?

var myTimer = null;

function startTimer() {
  myTimer = setInterval(function () {
    somefunction();
  }, 2000);
}

function stopTimer() {
  clearInterval(myTimer);
  myTimer =null;
}

document.addEventListener("DOMContentLoaded", function() {
  startTimer();
});

$('#idModalButton').click(function(){
      stopTimer();
      $('#idModal').modal('show');
    });

I also tried the following without success..same problem..only works in the first 2 seconds.

const myInterval = setInterval(myTimer, 2000);
    function myTimer() {
      somefunction();
    }
    

    $(document).on("click", "#idButtonModal", function(){ 
      clearInterval(myInterval);
      $('#idModal').modal('show');
    });
about 4 years ago · Juan Pablo Isaza
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!