Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 15th, 2012  |  syntax: None  |  size: 2.38 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to stop my javascript countdown?
  2. var settimmer = 0;
  3. $(function(){
  4.         window.setInterval(function() {
  5.             var timeCounter = $("b[id=show-time]").html();
  6.             var updateTime = eval(timeCounter)- eval(1);
  7.             $("b[id=show-time]").html(updateTime);
  8.         }, 1000);
  9.  
  10. });
  11.        
  12. <b id="show-time">20</b>
  13.        
  14. $(function(){
  15.         var timer = setInterval(function() {
  16.             var timeCounter = parseInt($("b[id=show-time]").text());
  17.             $("b[id=show-time]").text(--timeCounter); // remove one
  18.             if(!timeCounter) clearInterval(timer);
  19.         }, 1000);
  20. });
  21.        
  22. // Activate timer
  23. var iv = window.setInterval(...);
  24.  
  25. // Deactive timer
  26. window.clearInterval(iv);
  27.        
  28. $(function() {
  29.     // Read the start value once and store it in a variable
  30.     var timeCounter = parseInt( $("b[id=show-time]").text() );
  31.  
  32.     // Active the counter
  33.     var iv = window.setInterval(function() {
  34.         // Decrement by one and write back into the document
  35.         $("b[id=show-time]").text(--timeCounter);
  36.  
  37.         // Check if counter == 0 -> stop counting
  38.         if (0 == timeCounter) {
  39.             window.clearInterval(iv);
  40.             // ...do whatever else needs to be done when counter == 0 ..
  41.         }
  42.     }, 1000);
  43. });
  44.        
  45. var i = 0,
  46.     pid = setInterval(function() {
  47.         if (++i > 10)
  48.             clearInterval(pid);
  49.     }, 1000);
  50.        
  51. $(function() {
  52.     var el  = document.getElementById('show-time'),
  53.         pid = setInterval(function() {
  54.             // (s - i) coerces s to Number
  55.             var t = el.innerHTML - 1;
  56.             el.innerHTML = t;
  57.             if (t < 1)
  58.                 clearInterval(pid);
  59.         }, 1000);
  60. });
  61.        
  62. <script type="text/javascript">
  63. $(function(){
  64.  
  65. var settimmer = 0,
  66.     timeCounter = $("#show-time").html(),
  67.     updateTime = timeCounter;
  68.  
  69. (function countDown() {
  70.     timeCounter = $("#show-time").html();
  71.     updateTime = parseInt(timeCounter)-1;
  72.     $("#show-time").html(updateTime);
  73.     if ( updateTime ) {
  74.         setTimeout(countDown, 1000);
  75.     }
  76. })();
  77.  
  78. });​
  79. </script>
  80.        
  81. $(function(){
  82.     var elem=$('strong[id="show-time"]'),settimmer=0,updateTime,t;
  83.     t=window.setInterval(function() {
  84.         updateTime=parseFloat(elem.html(),10)-1;
  85.         if(updateTime==0) {
  86.             window.clearInterval(t);
  87.             elem.html('Done!');
  88.         } else {
  89.             elem.html(updateTime);
  90.         }
  91.     },1000);
  92. });
  93.        
  94. <strong id="show-time">20</strong>