3

The code below it counts minutes and seconds if the toggle button is clicked. Is it possible to make it cancel everything if its clicked/toggled, meaning that i can be able to clicked another toggle button.

function toggle(ths) {

    var clicked = $(ths).val();
    $("#lblType").html(clicked);
    $("#setCount").html(" minutes : " + minutes + " seconds : " + count);

       count = count + 1;
       if (count % 60 == 0) {
           minutes += 1;
           count = 0;
       }
        timer = setTimeout("toggle()", 1000);

    }




       <div ><label id="lblType"></label>
          <label id="setCount"></label>
     </div></p>
     <div id="planned"></div> 
Bonesh
  • 817
  • 2
  • 10
  • 17

5 Answers5

3

Just call clearTimeout():

clearTimeout(timer);
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
1

You can use clearTimeout()

clearTimeout(timer);

https://developer.mozilla.org/en/docs/DOM/window.clearTimeout

Darren
  • 68,902
  • 24
  • 138
  • 144
0

window.clearTimeout(timer)

Check http://www.w3schools.com/js/js_timing.asp for more info.

Kasyx
  • 3,170
  • 21
  • 32
0

By using cleartimeout(); clearinterval();

http://www.w3schools.com/jsref/met_win_cleartimeout.asp

Maresh
  • 4,644
  • 25
  • 30
0
var timer=null;

function toggle(ths) {
    var clicked = $(ths).val();
    $("#lblType").html(clicked);
    $("#setCount").html(" minutes : " + minutes + " seconds : " + count);

       count = count + 1;
       if (count % 60 == 0) {
           minutes += 1;
           count = 0;
       }
        timer = setTimeout("toggle()", 1000);

    }

function quitTimer(){
  window.clearTimeout(timer);
  timer = null;
}
Thomas Smart
  • 386
  • 3
  • 10