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

142
Views
stop interval inside a function

I have this script that runs a soccer game clock, when I click the start button everything is working properly but I can't stop the clock.

var count;

function gameClock() {

  var minutesLabel = document.getElementById("minutes");
  var secondsLabel = document.getElementById("seconds");
  var totalSeconds = 0;
  var count = setInterval(function() {
    setTime()
  }, 1000);

  function setTime() {
    ++totalSeconds;
    secondsLabel.innerHTML = pad(totalSeconds % 60);
    minutesLabel.innerHTML = pad(parseInt(totalSeconds / 60));
  }

  function pad(val) {
    var valString = val + "";
    if (valString.length < 2) {
      return "0" + valString;
    } else {
      return valString;
    }
  }
}



//Start Stop Game Clock
$(document).on('click', '#f_start', function() {

  $("#half_name").text('FIRST HALF');

  gameClock();
 //     localStorage.setItem('first_half_click', getTime());
 //     localStorage.setItem('half', 1);
})

$(document).on('click', '#f_stop', function() {

  clearInterval(count);

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="col-2">
  <button class="btn btn-success" id="f_start"> START</button>
</div>
<div class="col-2">
  <button class="btn btn-danger" id="f_stop"> STOP</button>
</div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are redeclaring the count global variable inside the gameClock method which means it's scope is only in that function. change the assignment to use the global count variable instead of the useless local variable.

 var count = setInterval(function() {
     setTime()
  }, 1000);

 //to
  count = setInterval(function() {
   setTime()
  }, 1000);

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!