I was working on a Timer code and was facing issue with it and posted a question regarding it. After discussion, thanks to @James, I found it was JSFiddle issue.
JSFiddle Link: http://jsfiddle.net/RajeshDixit/0gvfc5yy/4/
CodePen Link: http://codepen.io/rajesh_dixit/pen/RWBowQ
When you run it on JSFiddle, you will get following error
Uncaught ReferenceError: startTimer is not defined
But works fine on CodePen and in snippet below.
function timer() {
var time = {
sec: 00,
min: 00,
hr: 00
}
var max = 59;
var interval = null;
function update(str) {
time[str]++;
time[str] = time[str] % 60;
if (time[str] == 0) {
str == "sec" ? update("min") : update("hr");
}
print(str);
}
function print(str) {
var _time = time[str].toString().length == 1 ? "0" + time[str] : time[str];
document.getElementById("lbl" + str).innerHTML = _time;
}
function initInterval() {
interval = setInterval(function () {
update("sec");
}, 1000);
}
function stopTimer() {
clearInterval(interval);
}
return {
'init': initInterval,
'stop': stopTimer
}
};
var time = new timer();
function startTimer() {
time.init();
}
function endTimer() {
time.stop();
}
<div> <span id="lblhr">00</span>
: <span id="lblmin">00</span>
: <span id="lblsec">00</span>
</div>
<button onclick="startTimer()">Start</button>
<button onclick="endTimer()">Stop</button>