Skip to content Skip to sidebar Skip to footer

Javascript How To Pause/resume Function?

How to pause/resume odometer (so that it pauses/resumes)? I want to be able to pause/resume odometer respectively each time I click .pause-resume button. This is my code: https://j

Solution 1:

use setInterval function

var n = 0,
  timerID;
var myOdometer;
var div = document.getElementById("odometerDiv");
myOdometer = newOdometer(div, {
  value: n,
  digits: 6,
  tenths: true
});
myOdometer.set(0);

functionstartcounting() {


  timerID = setInterval(function() {
    n = n + 0.01
    myOdometer.set(n);
  }, 200);

}

//]]>startcounting();


var started = true;

$('button').click(function() {

  if (started)
    clearTimeout(timerID);

  elsestartcounting();

  started = !started;

  var currentvalue = myOdometer.get();
  $('#value').text(currentvalue);
});

fiddle

Solution 2:

var n = 0;
var timer;
var myOdometer;
functionstartcounting () {
var div = document.getElementById("odometerDiv");
myOdometer = newOdometer(div, {value: n, digits: 6, tenths: true});
myOdometer.set(0);  
update();
}

functionupdate () {
timer = setInterval(function() {
n = n + 0.01
myOdometer.set(n);}, 200);}
startcounting();
var state = true;
$('button').click(function() {
if (state)
clearTimeout(timer);
elseupdate();
state = !state;
var currentvalue = myOdometer.get();
$('#value').text(currentvalue);
});

Post a Comment for "Javascript How To Pause/resume Function?"